views:

730

answers:

5

Is there an elegant way to know when a worker thread is done executing so I can access resources it produced?

For example if the worker thread queried a list of SQL Servers using

ServersSqlDataSourceEnumerator.Instance.GetDataSources();

and saved the result in a DataTable variable, what mechanism can I use to know when this DataTable variable has been populated/is available. I don't want to poll ThreadState; it would be ideal to fire an event when it's done so I can perform actions with the result.

Thanks!

+1  A: 

So fire an event :-P

You could also look at using an AutoResetEvent:
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx

Joel Martinez
That would require some sort of polling that I'm trying to avoid. I'm looking for a solution that encapsulates thread creation, execution, and notification of completion. Being able to abort a thread is also a necessity.
Pwninstein
+1 for the "so fire an event" comment
ChrisF
+5  A: 

You can use a callback mechanism or block on an event to know of completion of an Async operation. See this page for the Asychronous Programming Model in .net - you can call BeginInvoke on any delegate to perform the action in an Async manner.

If you're using the BackgroundWorker type, you can subscribe to the RunWorkerCompleted event.

Gishu
+1 Ooh, very nice! I'm checking out the Asynchronous Programming Model link right now, and I may be able to use one of the examples listed there. Thanks!!
Pwninstein
A: 

I don't program in C# but here's what I did with Delphi, maybe you can do it as well with C#. I have a TThread descendant, and in the "destroy" event I send a message to its creator saying "hey I'm about to die !". This way its parent (which is the main thread) creates a new one if it needs a new one. To be precise it launches a timer that, when fired, creates a new thread if a new one is needed (sites sucking time (lol) !!).

Olivier Pons
+1  A: 

What I do in this instance is get the WorkerThread to call a function after it has completed the work, which will invoke the the UI Thread, which can do the work in which you require.

E.g.

private void SetWorkerThreadToDoWork()
{
  WorkerThread.Start();
}

private void MyWorkerThreadWork()
{
  //This will be on the WorkerThread (called from WorkerThread.Start())
  DoWorkFunc();
  WorkComplete();
}

private void WorkComplete()
{
  if(InvokeRequired == true)
  {
    //Do the invoke
  }
  else
  {
  //Check work done by worker thread
  //e.g. ServersSqlDataSourceEnumerator.Instance.GetDataSources();
  }
}

If it's a simple process you're using, I'd go for a BackgroundWorkerThread, this comes with it's own events that are fired when work is complete. But if you require to use a Thread, I would either look in to Asynchronous Callbacks or a similar route to that shown above.

ThePower
A: 

You can check my answer on this SO thread

It uses a call back mechanism. When the async operation is done, it will fire the callback method where you can handle the processing that needs to be done post SQL execution.

Use a similar approach to be notified when the asynchronous operation is done.

Hope this helps :)

Rashmi Pandit