Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to determine hanging or running of a thread at threadpool

Posted on 2003-10-26
13
Medium Priority
?
441 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 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
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…

722 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