2013-04-01 10 views

risposta

8

Se il metodo ha tornare uso valore Func delegato altrimenti è possibile utilizzare Action delegato. per esempio:

void Method1(string param) 
{ 
    // Some Code 
} 

void Method2(string param) 
{ 
    // Some Code 
} 

void RunInThread(Action<string> m) 
{ 
    //Run the method in a background thread 
} 

Poi si può chiamare RunInThread questo modo:

RunInThread(Method1); 
RunInThread(Method2); 
+0

Si potrebbe aggiungere 2 modi per eseguire in un thread separato: 'm.BeginInvoke (" Hello ", null, null);' e 'Task.Factory.StartNew (() => m (" Hello "));' –

2

mi piace Task.Run Quando voglio solo un po 'di codice per l'esecuzione in thread in background. Sembra addirittura che abbia quasi la stessa firma di quello che stai cercando di definire. Un sacco di altri sovraccarichi anche.

Task.Run(()=>{ 
     //background method code 
    }, TResult); 

MSDN documentation