Solved

How to determine hanging or running of a thread at threadpool

Posted on 2003-10-26
13
430 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:mscprojhk
  • 6
  • 6
13 Comments
 
LVL 1

Expert Comment

by:kellycoinguy
ID: 9625345
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
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9625528
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.
0
 

Author Comment

by:mscprojhk
ID: 9625590
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?
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9625685
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.
0
 

Author Comment

by:mscprojhk
ID: 9625699
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!
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9625715
So what happens when you kill the thread?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mscprojhk
ID: 9625742
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.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9625759
In the method oAlpha.Beta() you can get the current thread instance by reading Thread.CurrentThread.
0
 

Author Comment

by:mscprojhk
ID: 9625836
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
}
}
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9626343
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();
  }
0
 

Author Comment

by:mscprojhk
ID: 9631192
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?
0
 

Author Comment

by:mscprojhk
ID: 9631427
Do I need to write a tailor-made consumer-producer classes for this case?
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 50 total points
ID: 9632140
You could do something like:

foreadh(MyThreadCheckListEntry entry in myThreadCheckList)
{
    if ((DateTime.Now() - entry.LastCheckTime) > TimeSpan.FromSeconds(60))
    {
        // Thread is hanging
        entry.Thread.Abort();
    }
}
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now