4
Answers

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

Suraj Rai

Suraj Rai

17y
2.3k
1

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)