2011-09-29 57 views
2

我想要做的是能夠將函數引用傳遞給另一個函數,並將它用作System.Threading.ThreadPool.QueueUserWorkItem的回調方法。System.Threading.ThreadPool.QueueUserWorkItem的動態回調方法C#

參見方法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)); 
    } 

回答

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

謝謝,這正是我需要的。 – M3NTA7

+0

編譯器會將'(A)'變成'(new WaitCallback(A))'。 –

+0

是的,我只是老派。我想知道發生了什麼事。我喜歡var類型,節省時間並且清晰。 – Damon8or

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

是的,你想使用德使節:

public void CallbackDelegate(); 

private void D(CallbackDelegate D) 
{ 
} 
0

也許這可以幫助:

private void D(Delegate any) 
     {  
      System.Threading.ThreadPool.QueueUserWorkItem(ignored => any.DynamicInvoke()); 
     } 
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); 
     } 

    } 
} 
+0

謝謝,當我有更多時間時,我需要繼續。 – M3NTA7