Asynchronous call of a dynamically loaded method?
-
Hi, I'd like to invoke a couple of methods retrieved from multiple, dynamically loaded Assemblies (I have the MethodInfo objects) asynchronously and wait for all methods to finish processing before I continue. The method signature is: public void ProcessJob(JobInfo job); Can this be done? If so, how? Thanks in advance. Kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Hi, I'd like to invoke a couple of methods retrieved from multiple, dynamically loaded Assemblies (I have the MethodInfo objects) asynchronously and wait for all methods to finish processing before I continue. The method signature is: public void ProcessJob(JobInfo job); Can this be done? If so, how? Thanks in advance. Kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]matthias s. wrote:
asynchronously and wait for all methods to finish processing before I continue
:confused: If you want to wait till all methods finishes, what is the point in using asynchronous calls ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
matthias s. wrote:
asynchronously and wait for all methods to finish processing before I continue
:confused: If you want to wait till all methods finishes, what is the point in using asynchronous calls ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
they might take quite some time for processing and they should run parallel. once all of them are done, i need to move on.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
they might take quite some time for processing and they should run parallel. once all of them are done, i need to move on.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]Not tested this, but I believe this almost near to what you need to do. Assume you have
MethodInfo
array namedmethods
.WaitHandle[] waitHandles = new WaitHandle[methods.Length];
for(int i = 0;i < methods.Length; i++ )
{
waitHandles[i] = new AutoResetEvent(false);
MethodInfo info = methods[i];
new Thread(delegate(object userState)
{
info.Invoke();
(userState as AutoResetEvent).Set();
}).Start(waitHandles[i]);
}
WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
// when control reaches here, all methods will have finished executingAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Not tested this, but I believe this almost near to what you need to do. Assume you have
MethodInfo
array namedmethods
.WaitHandle[] waitHandles = new WaitHandle[methods.Length];
for(int i = 0;i < methods.Length; i++ )
{
waitHandles[i] = new AutoResetEvent(false);
MethodInfo info = methods[i];
new Thread(delegate(object userState)
{
info.Invoke();
(userState as AutoResetEvent).Set();
}).Start(waitHandles[i]);
}
WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
// when control reaches here, all methods will have finished executingAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thank you very much for your reply. That looks very good and I'll test it as soon as I've understood it. ;) What I don't get is where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for? Kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Thank you very much for your reply. That looks very good and I'll test it as soon as I've understood it. ;) What I don't get is where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for? Kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]matthias s. wrote:
where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for?
Well, you need to pass parameter to the
Invoke()
, right ? You could probably right an inner class and pass this classes instance to the user state, I mean as parameter to the thread'sStart()
method.private class DataCarrier
{
JobInfo JobInfoInstance { get; set; }
WaitHandle Handle { get; set; }DataCarrier(JobInfo info,WaitHandle handle){
this.JobInfoInstance = info;
this.Handle = handle;
}
}You can modify the above said method like
WaitHandle[] waitHandles = new WaitHandle[methods.Length];
for(int i = 0;i < methods.Length; i++ )
{
waitHandles[i] = new AutoResetEvent(false);
MethodInfo info = methods[i];
DataCarrier carrier = new DataCarrier(JobInfoInstance,waitHandles[i]);
new Thread(delegate(object userState)
{
DataCarrier c = userState as DataCarrier;
// use c.JobInfoInstance here
info.Invoke();
c.Handle.Set();
}).Start(carrier);
}
WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
// when control reaches here, all methods will have finished executingAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
matthias s. wrote:
where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for?
Well, you need to pass parameter to the
Invoke()
, right ? You could probably right an inner class and pass this classes instance to the user state, I mean as parameter to the thread'sStart()
method.private class DataCarrier
{
JobInfo JobInfoInstance { get; set; }
WaitHandle Handle { get; set; }DataCarrier(JobInfo info,WaitHandle handle){
this.JobInfoInstance = info;
this.Handle = handle;
}
}You can modify the above said method like
WaitHandle[] waitHandles = new WaitHandle[methods.Length];
for(int i = 0;i < methods.Length; i++ )
{
waitHandles[i] = new AutoResetEvent(false);
MethodInfo info = methods[i];
DataCarrier carrier = new DataCarrier(JobInfoInstance,waitHandles[i]);
new Thread(delegate(object userState)
{
DataCarrier c = userState as DataCarrier;
// use c.JobInfoInstance here
info.Invoke();
c.Handle.Set();
}).Start(carrier);
}
WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
// when control reaches here, all methods will have finished executingAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Don't get mad at me, but I don't fully understand the lines where the new thread is started.
N a v a n e e t h wrote:
new Thread(delegate(object userState) { DataCarrier c = userState as DataCarrier; // use c.JobInfoInstance here info.Invoke(); c.Handle.Set(); }).Start(carrier);
If I try to compile this in my code, I get the problem that I need to pass a parameter to the info.Invoke() method. Besides that, the WaitHandle doesn't contain a Set() method. Again, thanks for your help and patience.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Don't get mad at me, but I don't fully understand the lines where the new thread is started.
N a v a n e e t h wrote:
new Thread(delegate(object userState) { DataCarrier c = userState as DataCarrier; // use c.JobInfoInstance here info.Invoke(); c.Handle.Set(); }).Start(carrier);
If I try to compile this in my code, I get the problem that I need to pass a parameter to the info.Invoke() method. Besides that, the WaitHandle doesn't contain a Set() method. Again, thanks for your help and patience.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]matthias s. wrote:
WaitHandle doesn't contain a Set() method.
Yes. It doesn't contain a set method. It's my mistake, it should be "
EventWaitHandle
".private class DataCarrier
{
JobInfo JobInfoInstance { get; set; }
EventWaitHandle Handle { get; set; }DataCarrier(JobInfo info,EventWaitHandle handle){
this.JobInfoInstance = info;
this.Handle = handle;
}
}matthias s. wrote:
but I don't fully understand the lines where the new thread is started.
matthias s. wrote:
{ DataCarrier c = userState as DataCarrier; // use c.JobInfoInstance here info.Invoke(); c.Handle.Set(); }
Quoted one is the method which gets executed when thread starts. You can create a new method and put this there if you are confused with anonymous methods. I used it for simplicity.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions