Terminate a thread after an interval if not return

2019-04-19 09:47发布

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.

6条回答
你好瞎i
2楼-- · 2019-04-19 10:14

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.

查看更多
虎瘦雄心在
3楼-- · 2019-04-19 10:21

Either mechanism should allow you to specify a timeout on the read. For example, you could set NetworkStream.ReadTimeout to 5000 before performing the read. You should be able to do something similar for serial ports.

A read that has timed out will return 0 from Stream.Read() -- this is how you can tell if a timeout actually occurred.

Note that using Thread.Abort() is deprecated as of .NET 2.0 for some serious problems it can cause. You could theoretically use Thread.Interrupt(), but this will not do anything when the thread is blocked in unmanaged code (including I/O). Using read timeouts is the proper way to solve this problem.

查看更多
干净又极端
4楼-- · 2019-04-19 10:29

If you are using managed code to block the thread, you can use Thread.Abort to abort the execution. If you are using 3rd party code or COM interop, there is no way of terminating the thread execution. The only way is setting the Thread.isBackroundThread to true, and then terminating the Process...

查看更多
Ridiculous、
5楼-- · 2019-04-19 10:31

I guess a watcher thread is what you need. Start another thread at the same time and passes the serial port thread as parameter of that thread. Start the watcher with a five second sleep and check the status of the thread.

Now as for terminating the thread, see alex's answer, its just right !

查看更多
手持菜刀,她持情操
6楼-- · 2019-04-19 10:40

You mentioned you are reading from a "network". If you are using NetworkStream to read you can specify the ReadTimeout to be 5000 (it is in milliseconds). That will wait for 5 seconds then return after that if no data has been read. However if it is in the process or reading I think it might return also so be careful. I've never done anything with the serial API so I'm not sure if there is a setting in there like that.

查看更多
不美不萌又怎样
7楼-- · 2019-04-19 10:40

This is a two thread solution.

Example:

Thread tParent = new Thread(new ThreadStart(() =>
{
    Thread t = new Thread(AsyncMethod);
    t.Start();
    Thread.Sleep(5000);
    if (t.IsAlive) t.Abort();
}));

Remember:

It is a bad practice to use a method like this. Calling Thread.Abort() is not recommended approach. Use Thread Synchronization using WaitHandles instead. For example, AutoResetEvent.

查看更多
登录 后发表回答