Suraj Rai

Suraj Rai

  • NA
  • 111
  • 0

Can any one tell me what is the safe way of terminating a thread

Mar 20 2007 6:33 AM

I found that thread.Abort() does not terminate the thread properly. It
 
always throws ThreadAbortException...Can any one tell me how can I safely terminate a

thread.

My code of terminating thread....


private void btnCancel_Click(object sender, EventArgs e)
        {

            DialogResult dr = MessageBox.Show("Are you sure you want to terminate file backup process","Information",MessageBoxButtons.YesNo,MessageBoxIcon.Information);

            if (dr == DialogResult.Yes)
            {
                try
                {
                    //if (th.ThreadState == ThreadState.Running)
                    if(th.IsAlive)
                    {
                       
                        th.Abort();
                       // th.Join();
                        Thread.Sleep(1000);
                        this.Close();
                    }
                  
                   
                }
                catch (ThreadAbortException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
       

        }


After clicking Cancel if i call that funtion again then that thread does'nt start.

plz help....


Answers (4)