Solved

How to determine hanging or running of a thread at threadpool

Posted on 2003-10-26
13
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 87
how to remove duplicate code from my project 5 47
Name Space error VS2015 1 35
C# LINQ 5 19
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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