views:

178

answers:

5

I have a form that starts a thread. Now I want the form to auto-close when this thread terminates.

The only solution I found so far is adding a timer to the form and check if thread is alive on every tick. But I want to know if there is a better way to do that?

Currently my code looks more less like this

partial class SyncForm : Form {
    Thread tr;

    public SyncForm()
    {
        InitializeComponent();
    }

    void SyncForm_Load(object sender, EventArgs e)
    {
        thread = new Thread(new ThreadStart(Synchronize));
        thread.IsBackground = true;
        thread.Start();
        threadTimer.Start();
    }

    void threadTimer_Tick(object sender, EventArgs e)
    {
        if (!thread.IsAlive)
        {
            Close();
        }
    }

    void Synchronize()
    {
        // code here
    }
}
+6  A: 

The BackgroundWorker class exists for this sort of thread management to save you having to roll your own; it offers a RunWorkerCompleted event which you can just listen for.

Steve Gilham
Works great, thanks
RaYell
+2  A: 

Edit to make it call a helper method so it's cleaner.

thread = new Thread(() => { Synchronize(); OnWorkComplete(); });

...

private void OnWorkComplete()
{
    Close();
}
280Z28
+5  A: 

If you have a look at a BackgroundWorker, there is a RunWorkerCompleted event that is called when the worker completes.

For more info on BackgroundWorkers Click Here

Or

You could add a call to a complete function from the Thread once it has finished, and invoke it.

void Synchronize()
{
    //DoWork();
    //FinishedWork();
}

void FinishedWork()
{
if (InvokeRequired == true)
  {
  //Invoke
  }
else
  {
  //Close
  }
}
ThePower
+1  A: 

Have a look at delegates, IAsyncResult, BeginInvoke and AsyncCallback

astander
+1  A: 

At the end of your thread method, you can call Close() using the Invoke() method (because most WinForms methods should be called from the UI thread):

public void Synchronize()
{
   Invoke(new MethodInvoker(Close));
}
Philippe Leybaert