user2725580
user2725580

Reputation: 1313

detect a not responding thread and terminate it?

Sometimes the background thread runs slow and goes like, not responding, responding, not responding ...

Is it possible to detect that and then terminate it and then restart it? I make the threads like this. the Form3 just contains a web browser control which only navigates and search the web document.

Thread ab1 = new Thread(new ThreadStart(ThreadProc));
ab1.SetApartmentState(ApartmentState.STA);
ab1.Start();

private void ThreadProc()
{
    Form frm = new Form3(currentAB);
    frm.ShowDialog();

}

Upvotes: 1

Views: 1118

Answers (2)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

try this this will check only if the thread isalive but does not means that will detect if the thread is blocked on a long runing operation

 public partial class Form1 : Form
  {
     Thread ab1;   
     public Form1()
    {
         InitializeComponent();
        Thread ab1 = new Thread(new ThreadStart(ThreadProc));
        ab1.SetApartmentState(ApartmentState.STA);
        ab1.Start();
        bool isAlive = true ;
        TimerCallback tmrCallBack = new TimerCallback(CheckStatusThreadHealth);  
        System.Threading.Timer tmr = new System.Threading.Timer(tmrCallBack,isAlive,2000,2000);
     }
     public void CheckStatusThreadHealth(object isAlive)
     {
         isAlive = ab1.IsAlive ;  
     }
     private void ThreadProc()
     {
         Form frm = new Form1();
        frm.ShowDialog();

     }

hope this help

Upvotes: 0

shat90
shat90

Reputation: 405

One alternative is to use a Timer on a Thread and call it every certain amount of seconds, check if your thread is dead.

Systems.Threading.Timer class would help you in that

http://msdn.microsoft.com/en-us/library/vstudio/swx5easy.aspx

Upvotes: 3

Related Questions