Solved

How to determine hanging or running of a thread at threadpool

Posted on 2003-10-26
13
440 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

630 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