2

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!

raven
  • 18,004
  • 16
  • 81
  • 112
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149

5 Answers5

5

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
  • 134,492
  • 47
  • 225
  • 308
  • +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!! – Mark Carpenter Jun 03 '09 at 14:57
1

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
  • 46,929
  • 26
  • 130
  • 185
  • 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. – Mark Carpenter Jun 03 '09 at 14:38
1

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.

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
1

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 :)

Community
  • 1
  • 1
Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111
0

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
  • 15,363
  • 26
  • 117
  • 213