Threading question
-
I am trying to figure out how to sent an event or set a callback when a thread finishes its work. Lets say that I have 3 functions called:
1) GetData1() // Gets a list of items ( id, name, age ) 2) GetData2() // Gets an item where id = an id from the list from GetData1() ( num, color, etc ) 3) GetData3() // Gets an item where color = color from the list in from GetData2()
Each function depends on the results of the prior function in order to be able to do its work. These threads cannot block the main application thread.However, GetData1() may block GetData2() and GetData3() and GetData2() can block GetData3() Each of these worker threads make calls to and XML server, and when a given thread finishes I need to start the next one. The threads can take a long time to complete their task, meaning minutes and not milliseconds etc. How can I know when a previous thread has finsihed? Thanks -- modified at 15:10 Wednesday 14th December, 2005You can pass a delegate into the various BeginInvoke methods to have a function execute when the thread is done. For example,
ThreadStart method1 = GetData1();
method1.BeginInvoke(myCallbackFunction, null);...
void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!
}Does this help? Or am I not understanding your problem?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango
-
I am trying to figure out how to sent an event or set a callback when a thread finishes its work. Lets say that I have 3 functions called:
1) GetData1() // Gets a list of items ( id, name, age ) 2) GetData2() // Gets an item where id = an id from the list from GetData1() ( num, color, etc ) 3) GetData3() // Gets an item where color = color from the list in from GetData2()
Each function depends on the results of the prior function in order to be able to do its work. These threads cannot block the main application thread.However, GetData1() may block GetData2() and GetData3() and GetData2() can block GetData3() Each of these worker threads make calls to and XML server, and when a given thread finishes I need to start the next one. The threads can take a long time to complete their task, meaning minutes and not milliseconds etc. How can I know when a previous thread has finsihed? Thanks -- modified at 15:10 Wednesday 14th December, 2005Sounds rather complex, let me think: Every process should have some kind of semaphore. GetData2 should call wait on the semaphore of GetData1. GetData1 should call signal on the semaphore it has, so that GetData2 continues. GetData3 should call wait on the semaphore of GetData2 and GetData2 should call signal on the semaphore it has, to let GetData3 continue. This should work just fine and you dont have to worry about a lot of the synchronisation here :) There are other ways to do this, but this is by far the simplest solution I can think of. WM.
What about weapons of mass-construction? -- modified at 15:21 Wednesday 14th December, 2005 (Fixed some minor spelling issues ;P) -
You can pass a delegate into the various BeginInvoke methods to have a function execute when the thread is done. For example,
ThreadStart method1 = GetData1();
method1.BeginInvoke(myCallbackFunction, null);...
void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!
}Does this help? Or am I not understanding your problem?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango
-
You can pass a delegate into the various BeginInvoke methods to have a function execute when the thread is done. For example,
ThreadStart method1 = GetData1();
method1.BeginInvoke(myCallbackFunction, null);...
void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!
}Does this help? Or am I not understanding your problem?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango
This seems to be what I am looking for. The snipit you provided doesn't compile, but I can look up the syntax.
-
This seems to be what I am looking for. The snipit you provided doesn't compile, but I can look up the syntax.
It should compile under .NET 2.0 syntax (where delegates can be inferred from the method passed). If you're not using a C# 2.0 compiler, you'll need to explicitly create the delegates:
ThreadStart method1 = new ThreadStart(GetData1);
AsyncCallback callback = new AsyncCallback(myCallbackFunction);
method1.BeginInvoke(callback, null);...
void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango
-- modified at 15:40 Wednesday 14th December, 2005
-
It should compile under .NET 2.0 syntax (where delegates can be inferred from the method passed). If you're not using a C# 2.0 compiler, you'll need to explicitly create the delegates:
ThreadStart method1 = new ThreadStart(GetData1);
AsyncCallback callback = new AsyncCallback(myCallbackFunction);
method1.BeginInvoke(callback, null);...
void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango
-- modified at 15:40 Wednesday 14th December, 2005
Thanks that did the trick. Sorry, but I am still in the stone ages using .net 1.1 and Visual Studio 2003 :) Thanks again
-
I am trying to figure out how to sent an event or set a callback when a thread finishes its work. Lets say that I have 3 functions called:
1) GetData1() // Gets a list of items ( id, name, age ) 2) GetData2() // Gets an item where id = an id from the list from GetData1() ( num, color, etc ) 3) GetData3() // Gets an item where color = color from the list in from GetData2()
Each function depends on the results of the prior function in order to be able to do its work. These threads cannot block the main application thread.However, GetData1() may block GetData2() and GetData3() and GetData2() can block GetData3() Each of these worker threads make calls to and XML server, and when a given thread finishes I need to start the next one. The threads can take a long time to complete their task, meaning minutes and not milliseconds etc. How can I know when a previous thread has finsihed? Thanks -- modified at 15:10 Wednesday 14th December, 2005Although it seems you already found a solution just another question: If those three threads get executed sequentially (one after the other) why don't you just call all three methods in a single thread?
-
It should compile under .NET 2.0 syntax (where delegates can be inferred from the method passed). If you're not using a C# 2.0 compiler, you'll need to explicitly create the delegates:
ThreadStart method1 = new ThreadStart(GetData1);
AsyncCallback callback = new AsyncCallback(myCallbackFunction);
method1.BeginInvoke(callback, null);...
void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango
-- modified at 15:40 Wednesday 14th December, 2005
Well, it compiles but it doesn't ever call the callback function Here is my callback function
void MyCallbackFunction(IAsyncResult result) { // GetData1 is done!} if( result.IsCompleted ) MessageBox.Show( "Callback triggered"); }
Here is how I am calling itThreadStart method1 = new ThreadStart(RequestVehicles); AsyncCallback callback = new AsyncCallback(MyCallbackFunction); method1.BeginInvoke(callback, null); Thread tThread = new Thread( method1 ); tThread.IsBackground = true; tThread.Start();
Have I done something wrong? -- modified at 17:19 Wednesday 14th December, 2005 -
Although it seems you already found a solution just another question: If those three threads get executed sequentially (one after the other) why don't you just call all three methods in a single thread?
Worth a look, thanks
-
Well, it compiles but it doesn't ever call the callback function Here is my callback function
void MyCallbackFunction(IAsyncResult result) { // GetData1 is done!} if( result.IsCompleted ) MessageBox.Show( "Callback triggered"); }
Here is how I am calling itThreadStart method1 = new ThreadStart(RequestVehicles); AsyncCallback callback = new AsyncCallback(MyCallbackFunction); method1.BeginInvoke(callback, null); Thread tThread = new Thread( method1 ); tThread.IsBackground = true; tThread.Start();
Have I done something wrong? -- modified at 17:19 Wednesday 14th December, 2005In that code snippet, you're calling RequestVehicles 2 times: once when you call method1.BeginInvoke method, and again when you explicitly create your Thread and call .Start on the thread. Just remove that explicit thread creation, it's redundant. Your code should look like:
ThreadStart method1 = new ThreadStart(RequestVehicles);
AsyncCallback callback = new AsyncCallback(MyCallbackFunction);
method1.BeginInvoke(callback, null);void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!}
if( result.IsCompleted )
MessageBox.Show( "Callback triggered");
}Note that if you used
Thread t = ...
t.Start()it will not call any callback method. Only BeginInvoke does that.
-
In that code snippet, you're calling RequestVehicles 2 times: once when you call method1.BeginInvoke method, and again when you explicitly create your Thread and call .Start on the thread. Just remove that explicit thread creation, it's redundant. Your code should look like:
ThreadStart method1 = new ThreadStart(RequestVehicles);
AsyncCallback callback = new AsyncCallback(MyCallbackFunction);
method1.BeginInvoke(callback, null);void MyCallbackFunction(IAsyncResult result)
{
// GetData1 is done!}
if( result.IsCompleted )
MessageBox.Show( "Callback triggered");
}Note that if you used
Thread t = ...
t.Start()it will not call any callback method. Only BeginInvoke does that.
Thanks I will try that directly
-
Although it seems you already found a solution just another question: If those three threads get executed sequentially (one after the other) why don't you just call all three methods in a single thread?
I used a combination of both tips
ThreadStart method1 = new ThreadStart(DoRequests); AsyncCallback callback = new AsyncCallback(MyCallbackFunction); method1.BeginInvoke(callback, null); `` void DoRequests() { RequestVehicles(); RequestCustomers(); RequestServiceSales(); } `` void MyCallbackFunction(IAsyncResult result) { if( result.IsCompleted ) { // All data received add to database or whatever } }
Thanks All