ianinspain
asked on
500pts: Callback for a thread?
Hi there,
I wonder if anyone can help, I have a thread which i have created which basically goes off and runs a Method which sends an email.... In the meantime i show a form that says "Sending email...." modally..
What i need to do is receive some sort of callback when its finsihed so that i can close the form "Sending email...."
If anybody can help that would be really appreciated.....
THanks in advance.. Ian..
Here is my method from my form...
SendError sendError = new SendError(exceptionData, exceptionManagers); // this is a class that does email sending
Thread tid1 = new Thread(new ThreadStart( sendError.Run ) );
tid1.Start();
this.ShowDialog();
then i suppose - so some sort of callback to say "Finsihed"
public void Finished()
{
this.close();
}
I wonder if anyone can help, I have a thread which i have created which basically goes off and runs a Method which sends an email.... In the meantime i show a form that says "Sending email...." modally..
What i need to do is receive some sort of callback when its finsihed so that i can close the form "Sending email...."
If anybody can help that would be really appreciated.....
THanks in advance.. Ian..
Here is my method from my form...
SendError sendError = new SendError(exceptionData, exceptionManagers); // this is a class that does email sending
Thread tid1 = new Thread(new ThreadStart( sendError.Run ) );
tid1.Start();
this.ShowDialog();
then i suppose - so some sort of callback to say "Finsihed"
public void Finished()
{
this.close();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
nice point. :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow thanks guys, its working great.. i used this
private void sendError_Finished(object sender, EventArgs e)
{
if (this.InvokeRequired)
this.Invoke(new ThreadFinishedDelegate(thi s.Close));
else
this.Close();
}
Something i am a little curious about is that should i not Stop the thread, i mean technically its finished but does this mean its still loaded in memory... should i not use stop?? after this
Thread tid1 = new Thread(new ThreadStart( sendError.Run ) );
tid1.Start();
private void sendError_Finished(object sender, EventArgs e)
{
if (this.InvokeRequired)
this.Invoke(new ThreadFinishedDelegate(thi
else
this.Close();
}
Something i am a little curious about is that should i not Stop the thread, i mean technically its finished but does this mean its still loaded in memory... should i not use stop?? after this
Thread tid1 = new Thread(new ThreadStart( sendError.Run ) );
tid1.Start();
ASKER
oh i just noticed there is no STOP method, so should i just set tid1 = null
or just forget about it?
Ian
or just forget about it?
Ian
Since event handler is called when thread is actually exits, you don't need to stop it. You can waut for thread exit using tid1.Join(); if you want to be 100% sure that thread ended.
When thread exits, underlying Win32 thread is destroyed, but .NET Thread object is still alive because it is referenced by td1 variable. You can set this variable to null and it is released on next Garbage Collector iteration.
When thread exits, underlying Win32 thread is destroyed, but .NET Thread object is still alive because it is referenced by td1 variable. You can set this variable to null and it is released on next Garbage Collector iteration.
// OnThreadFinished event handler
public void Finished()
{
this.BeginInvoke(new ThreadFinished(this.Close)
}
because Form.Close has the same type as ThreadFinished delegate: void ()