Link to home
Start Free TrialLog in
Avatar of mscprojhk
mscprojhk

asked on

How to determine hanging or running of a thread at threadpool

As subject, how can I know that the thread is hanging or still running?

My implementation is setting a timeout, so that an event will be passed back to the application either timeout or all the threads finish.

Is it sufficient enough that my application gets the available thread number by using ThreadPool.GetAvailableThreads. If it's equal to 25 (by default), that means all the threads are finishing. But, the problem is that I can't really kill the "hanging" thread at the thread pool. I only saw the coding at the instance thread by using abort() method.


Thanks!

Cheers,
Mullin
Avatar of kellycoinguy
kellycoinguy

The answer is probably somewhat dependent upon what might be "hanging" the thread. If you are waiting on a remote machine, or a user, then a timeout is probably a reasonable way of dealing with things. If on the other hand, it might be "hanging" because of deadlock, or some other deficiency in the coding style, then I'd encourage you to restructure the code so that it doesn't occur.

As for killing the waiting thread, you could probably create some shared memory structure that all threads periodically look at, and if a particular boolean is set, the thread exits itself. I am not a threading guru, so I probably can't help with the specifics of doing this, but it shouldn't be too terribly difficult if I understand things correctly.

-Kelly
Yes, what is the thread doing (resp. should it be doing) and why could it be hanging. Most problems are caused by: deadlocks and network connections closed by remote host.
Avatar of mscprojhk

ASKER

Yes, I want to write the logic to handle those unexpected cases (deadlock and time-out issue).

But, I couldn't find way to delete the "Working" / "Deadlock" threads at the ThreadPool class. Also, there's no way to reset the all threadpool (delete all).

Any ideas?
Do you have deadlocks or you just want to detect them? If you have then look for the reason. If you want to detect them then let each thread update a field in a hashtable (key=tread id / value = DateTime.Now()). Now you need a thread checking the entries. If one is too old, kill the specified thread and log it (else you'll never find out). If the whole process is hanging you need to do the same between processes.
thanks for your solution. but, technically, i can't find way to delete a thread or a threadpool!! it seems to be no methods. i tried with the .net built-in ThreadPool and another guy's implementation ManagedThreadPool, but none of them have this method.

what's can i do? neglecting the thread pool??? i don't think so.

thanks!
So what happens when you kill the thread?
no, i don't know how to kill a thread if i use ThreadPool.

if just a Thread instance, i can just use thread.abort() to terminate it. but, there's no method if i use threadpool like

ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(tmpJobIDArray[iItem]));

a thread will be created automatically that i don't know how to specify the specific thread, and the method to kill it as i didn't instantiate a Thread instance indeed.
In the method oAlpha.Beta() you can get the current thread instance by reading Thread.CurrentThread.
thanks! sorry that may be i'm not very good at it.

oAlpha.Beta() should be the method that multiple threads are running at, but i have a controller application. the point is that i want the main application knows which thread or all the threads to be deleted/reset.

Thread.CurrentThread.abort() is running outside the main application.

public class Alpha
{
   public Alpha(int MaxCount)
  {
    .......
  }
  // Beta is the method that will be called when the work item is
  // serviced on the thread pool.
  // That means this method will be called when the thread pool has
  // an available thread for the work item.
  public void Beta(Object state)
  {                  
     .......
  }

public class SimplePool
{
public static int Main(string[] args)
{
   .......
   ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(tmpJobIDArray[iItem]));
   .......

    // want to check whether the threads are complete or hanging/time-out
   if (time-out)
      // reset a working thread or all working threads
      // roll-back mechanism
}
}
I imagine about something like that:
  public void Beta(Object state)
  {              
      // add or look up your thread in a global, threadsafe list
      myThreadCheckList.Lookup(Thread.CurrentThread).LastCheckTime = DateTime.Now();
  }
If I know that a thread "abc" doesn't return anything from the lookup=> it's eithe processing or hanging or deadlock

how can i kill it from the main application?
Do I need to write a tailor-made consumer-producer classes for this case?
ASKER CERTIFIED SOLUTION
Avatar of ptmcomp
ptmcomp
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial