Dynamic callback method for System.Threading.ThreadPool.QueueUserWorkItem C#
What I am trying to do is be able to pass a function reference to another function and have it used as a callback method for a System.Threading.ThreadPool.QueueUserWorkItem.
See in method D() the 'Any' parameter.
I need to be able to pass the callback method pointer or reference for the 'Any' parameter. I can't use a delegate, because that would need to be static, is that correct?
Any ideas?
private void A() { /*code*/ }
private void B() { /*code*/ }
private void C(int i)
{
switch(i)
{
case 1:
D(A());
break;
case 2:
D(B());
break;
default:
break;
}
}
private void D(type? Any)
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCall开发者_JAVA百科back(Any));
}
I can't use a delegate, because that would need to be static, is that correct.
No that is not correct.
delegate void MyMethods();
class Foo
{
void Minstance() {}
static void Mstatic() {}
MyMethods m1 = Minstance; // OK
MyMethods m2 = Mstatic; // OK
}
And the following is incorrect syntax:
case 1:
D(A()); // here you call (execute) A
break;
just omit the parenthesis after the method:
case 1:
D(A); // this passes a reference to A
break;
And now you have to properly define D :
void D(WaitCallback any)
{
ThreadPool.QueueUserWorkItem(any);
}
I think this will do what you want, but WaitCallback delegates take an object as a parameter.
private void A(object state)
{
// does one thing
}
private void B(object state)
{
// does a different thing
}
private void C(int i)
{
switch (i)
{
case 1:
D(new System.Threading.WaitCallback(A));
break;
case 2:
D(new System.Threading.WaitCallback(B));
break;
default:
break;
}
}
private void D(System.Threading.WaitCallback worker)
{
System.Threading.ThreadPool.QueueUserWorkItem(worker);
}
Yes, you want to use a delegate:
public void CallbackDelegate();
private void D(CallbackDelegate D)
{
}
Maybe that could help:
private void D(Delegate any)
{
System.Threading.ThreadPool.QueueUserWorkItem(ignored => any.DynamicInvoke());
}
Try using an delegate like here. Please ask me if you have any questions.
using System;
using System.Threading;
namespace Thread.Pool.Test
{
delegate void VoidDelegate (object obj);
public class Delegate
{
/// <summary>
/// ThreadPool Entry Point for A
/// </summary>
/// <param name='obj'>
/// EventWaitHandle
/// </param>
/// <exception cref='ArgumentException'>
/// Is thrown when an argument passed to a method is invalid.
/// </exception>
private void A (object obj) {
if (!(obj is EventWaitHandle))
throw new ArgumentException ("Only EventWaitHandle supported!");
A ((EventWaitHandle)obj);
}
private void A (EventWaitHandle handle) {
// does one thing
//finsihed
handle.Set ();
}
/// <summary>
/// ThreadPool Entry Point for B
/// </summary>
/// <param name='obj'>
/// EventWaitHandle
/// </param>
/// <exception cref='ArgumentException'>
/// Is thrown when an argument passed to a method is invalid.
/// </exception>
private void B (object obj) {
if (!(obj is EventWaitHandle))
throw new ArgumentException ("Only EventWaitHandle supported!");
B ((EventWaitHandle)obj);
}
private void B (EventWaitHandle handle) {
// does a different thing
//finsihed
handle.Set ();
}
private void C (int i) {
EventWaitHandle waitHandle = new ManualResetEvent (false);
switch (i) {
case 1:
D (A ,waitHandle);
break;
case 2:
D (B ,waitHandle);
break;
default:
throw new Exception ("Case not supported");
}
//Wait for the thread to finish
waitHandle.WaitOne ();
}
private void D (VoidDelegate any, EventWaitHandle waitHandle) {
ThreadPool.QueueUserWorkItem (new System.Threading.WaitCallback (any),waitHandle);
}
}
}
精彩评论