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....
Mike GoldPosted Mar 21, 2007, 6:16 PM
Delegate void CloseFormDelegate();
...
CloseForm = new CloseFormDelegate(OnMyEvent);
void OnMyEvent()
{
this.Close()
}
// user cancelled, call this in the thread after it exits the loop
this.Invoke(CloseForm);
Suraj RaiPosted Mar 21, 2007, 1:19 AM
Tnx for the reply to both of u....
I am getting exception in thread.Abort() not in thread.Sleep()...
Mike ...abt ur code i m not getting where to place this code...
Actually my application uploads file to server . So wat i m doing is I am calling a
function in saparate thread for uploading files like this...
private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Step = 1;
progressBar1.Value = 0;
th = new Thread(new ThreadStart(CalBAckUpFrm));
th.Start();
}
public void CalBAckUpFrm()
{
// Uploading codes
}
I ve a Cancel button...If user wants to cancel the process in the middle the thread should get
terminated....
By luking at ur code i did some changes..like this...
public bool boolThreadTerminator = true;
public void CalBAckUpFrm()
{
while (boolThreadTerminator)
{
//Uploading codes
}
}
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.IsAlive)
{
boolThreadTerminator = false;
this.Close();
}
}
catch (ThreadAbortException ex)
{
MessageBox.Show(YuntaaClasses.Exceptions.HasIOException(ex), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
and its not working....when i cancel the form the form gets closed but the uploading
still continues in background.
I think I did'nt get u properly....
Can u plz help me little....
Mike GoldPosted Mar 20, 2007, 7:38 PM
for example
bool _flag = true;
void mythreadhandler()
{
while (_flag)
{
}
}
Setting the flag to false will terminate the loop, exit the method and terminate the thread.
-Mike G.
Ripal SoniPosted Mar 20, 2007, 8:51 AM
did you debug your code?
it will give you the exact place where you are getting this exception on thread.abort or on thread.sleep
if you are creating new thread at the starting of thread then thread.abort should work
try comment out that thread.sleep line
Good Luck!