Link to home
Start Free TrialLog in
Avatar of ianinspain
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();  
 }
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

Right, my sample is not correct: this.Close is executed in the context of worker thread. Quick and dirty correction can be:

// OnThreadFinished event handler
public void Finished()
{
            this.BeginInvoke(new ThreadFinished(this.Close));  
 }

because Form.Close has the same type as ThreadFinished delegate: void ()
nice point. :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ianinspain

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(this.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();

oh i just noticed there is no STOP method, so should i just set tid1 = null

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.