I'm new to threading in C#. Is there anyway of setting a timeout for a thread without blocking the calling thread (in C# 3.5)?
If not, is it logical to execute a function using a thread and within that function create a thread and join it to overcome this main thread blocking issue? To illustrate:
Instead of:
Public void main()
{
...
Thread thrd1 = new Thread(new ThreadStart(targetObj.targetFunc));
thrd1.Start();
thrd1.Join();
...
}
Using something like:
Public void main()
{
...
Thread thrd1 = new Thread(new ThreadStart(middleObj.waiter));
thrd1.Start();
...
}
//And in the middleObj.waiter():
Public void waiter()
{
Thread thrd2 = new Thread(new ThreadStart(targetObj.targetFunc));
thrd2.Start();
thrd2.Join();
}
I checked and the simplest and most comprehensive way of doing it was the solution I mentioned in the description of the question. A middle level thread can easily wait for the second thread without any interruption over the main thread; and it can kill the second thread if it does not respond within the required time. That's exactly what I needed. I used it and it worked without a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With