System.Threading.ThreadPool.QueueUserWorkItem C#的动态回调方法

时间:2011-09-29 20:48:56

标签: c# .net multithreading

我要做的是能够将函数引用传递给另一个函数,并将其用作System.Threading.ThreadPool.QueueUserWorkItem的回调方法。

参见方法D()中的'Any'参数。

我需要能够传递'Any'参数的回调方法指针或引用。我不能使用委托,因为那需要是静态的,这是正确的吗?

有什么想法吗?

    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.WaitCallback(Any));
    }

5 个答案:

答案 0 :(得分:3)

  

我不能使用委托,因为那需要是静态的,这是正确的。

不,这不正确。

delegate void MyMethods();

class Foo
{
    void Minstance() {}
    static void Mstatic() {}

    MyMethods m1 = Minstance;  // OK
    MyMethods m2 = Mstatic;    // OK
}

以下是不正确的语法:

        case 1:
            D(A());  // here you call (execute) A
            break;

在方法之后省略括号:

        case 1:
            D(A);    // this passes a reference to A
            break;

现在你必须正确定义D:

void D(WaitCallback any)
{
      ThreadPool.QueueUserWorkItem(any);
}

答案 1 :(得分:2)

我认为这会做你想要的,但是WaitCallback委托将一个对象作为参数。

        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);
    }

答案 2 :(得分:0)

是的,您想使用委托:

public void CallbackDelegate();

private void D(CallbackDelegate D)
{
}

答案 3 :(得分:0)

也许这可能会有所帮助:

private void D(Delegate any)
        {    
            System.Threading.ThreadPool.QueueUserWorkItem(ignored => any.DynamicInvoke());
        }

答案 4 :(得分:0)

尝试使用此处的委托。如果您有任何问题,请询问我。

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);
        }

    }
}