views:

83

answers:

4

I have I multi thread application written by c#, my max thread number is 256 and this application gets the performance counters of the computers in an Ip interval(192.168.1.0 -192.168.205.255) it works fine and turns many times in a day. because I have to get reports.

But the problem is some times one machine keeps a thread and never finishes its work so my loop doesnt turn...

Are there any way to create threads with a countdown parameter. when I start the threads in foreach?

foreach(Thread t in threads)
{
   t.start(); -----> t.start(countdownParameter) etc....
}

coundown parameter is the max life of each threads. This mean if a thread cant reach a machine it have to be abort. for example 60 seconds.. no not 256 machines, I meant 256 threads... there are about 5000 ip and 600 of them are alive. soo I am using 256 threads to read their values. and the other thing is loop. my loop is working as while all off the ipies finish it starts from beginning.

A: 

You can't specify a timeout for thread execution. However, you can try to Join each thread with a timeout, and abort it if it doesn't exit.

foreach(Thread t in threads)
{
   t.Start();
}

TimeSpan timeOut = TimeSpan.FromSeconds(10);
foreach(Thread t in threads)
{
   if (!t.Join(timeOut))
   {
       // Still not complete after 10 seconds, abort
       t.Abort();
   }
}

There are of course more elegant ways to do it, like using WaitHandles with the WaitAll method (note that WaitAll is limited to 64 handles at a time on most implementations, and doesn't work on STA threads, like the UI thread)

Thomas Levesque
I tried this code but all of the threads are waiting the finish the thread which created before it. and it is not useful solution.
Rapunzo
A: 

You should not terminate the thread from the outside. (Never kill a thread, make it commit suicide). Killing a thread can easily corrupt the state of an appdomain if you're not very careful.

You should rewrite the network code in the threads to either time out once the time-limit has been reached, or use asynchronous network code.

CodeInChaos
I tried to control worktime with a timer but it blocked my operation. what kind of solution do you offer?
Rapunzo
Socket APIs typically have a TimeOut parameter/property for blocking mode(SendTimeOut and ReceiveTimeOut on the .net Socked class for example), and a way to enter non blocking(=asynchronous) mode(Blocking property on Socket).
CodeInChaos
"Killing a thread can easily corrupt the state of an appdomain" perhaps it's true for OS threads, but I doubt aborting a managed thread would have that kind of effect... I just raises a ThreadAbortException on the aborted thread.
Thomas Levesque
A: 

Hi! You can use smth like this:

public static T Exec<T>(Func<t> F, int Timeout, out bool Completed)
{
    T result = default(T);
    Thread thread = new Thread(() => result = F());
    thread.Start();
    Completed = thread.Join(Timeout);
    if(!Completed) thread.Abort();
    return result;
}
BrainMaxx
A: 

Usually a thread gets stuck on a blocking call (unless of course you have a bug causing an infinite loop). You need to identify which call is blocking and "poke" it to get it to unblock. It could be that your thread is waiting inside one of the .NET BCL waiting calls (WaitHandle.WaitOne, etc.) in which case you could use Thread.Interrupt to unblock it. But, in your case it is more likely that the API managing the communication with the remote computers is hung. Sometimes you can simply close the connection from a separate thread and that will unblock the hung method (as is the case with the Socket class). If all else fails then you really might have to fall back on the method of last of calling Thread.Abort. Just keep in mind that if you abort a thread it might corrupt the state of the app domain in which the abort originated or even the entire process itself. There were a lot of provisions added in .NET 2.0 that make aborts a lot safer than they were before, but there is still some risk.

Brian Gideon