Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:21:25+00:00 2026-05-18T20:21:25+00:00

I have a thread which grabs some data from network or serial port. The

  • 0

I have a thread which grabs some data from network or serial port. The thread must terminate (or return false) if no data is received within 5 seconds.

In other words, if running the thread is taking more than 5 seconds it must stop.

I am writing in C#, but any .NET language is OK.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:21:25+00:00Added an answer on May 18, 2026 at 8:21 pm

    There are two approaches:

    1. Encapsulated timeout

    The thread reading the data from network or serial port can measure time elapsed from its time of start and wait for the data for no more than the remaining time. Network communication APIs usually provide means to specify a timeout for the operation. Hence by doing simple DateTime arithmetic you can encapsulate timeout management within your worker thread.

    2. External timeout

    Use another thread (or do it in the main thread if that’s feasible) to wait for the worker thread to finish within a certain time limit, and if not, abort it. Like this:

    // start the worker thread
    ...
    
    // give it no more than 5 seconds to execute
    if (!workerThread.Join(new TimeSpan(0, 0, 5)))
    {    
        workerThread.Abort();
    }
    

    Recommendation: I’d stick with the first solution, as it leads to cleaner and maintainable design. However, in certain situation it might be necessary to provide means for ‘hard’ abort of such worker threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread which grabs some data from network or serial port. The
I have a thread which produces some data (a python list) and which shall
I have a thread which must wait several objects from different threads. @Override public
I have the main thread from which I start a window using invokeLater .
I have a parent thread (non-UI) which creates some child threads to do some
I have a parent thread (non-UI) which creates some child threads to do some
I have a thread which can print some text on the console and the
I have this thread which downloads a few images from the server. So once
I have a thread which performs some tasks. At the end I want to
I have a thread which produces data in the form of simple object (record).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.