2009-04-23 77 views
12

從我在C#中找到的,Control.Invoke方法要求您使用沒有輸入參數的委託。有沒有辦法解決?我想調用一個方法來從另一個線程更新UI並傳遞給它的字符串參數。Control.Invoke與輸入參數

回答

22

您正在使用哪種版本的C#?如果您使用的是C#3.5,則可以使用閉包來避免傳入參數。

用C#3.5
public static class ControlExtensions 
{ 
    public static TResult InvokeEx<TControl, TResult>(this TControl control, 
              Func<TControl, TResult> func) 
    where TControl : Control 
    { 
    return control.InvokeRequired 
      ? (TResult)control.Invoke(func, control) 
      : func(control); 
    } 

    public static void InvokeEx<TControl>(this TControl control, 
             Action<TControl> func) 
    where TControl : Control 
    { 
    control.InvokeEx(c => { func(c); return c; }); 
    } 

    public static void InvokeEx<TControl>(this TControl control, Action action) 
    where TControl : Control 
    { 
    control.InvokeEx(c => action()); 
    } 
} 

安全現在調用代碼變得微不足道。

this.InvokeEx(f => f.label1.Text = "Hello World"); 
this.InvokeEx(f => this.label1.Text = GetLabelText("HELLO_WORLD", var1)); 
this.InvokeEx(() => this.label1.Text = DateTime.Now.ToString()); 

用C#2.0它變得越來越微不足道
public class MyForm : Form 
{ 
    private delegate void UpdateControlTextCallback(Control control, string text); 
    public void UpdateControlText(Control control, string text) 
    { 
    if (control.InvokeRequired) 
    { 
     control.Invoke(new UpdateControlTextCallback(UpdateControlText), control, text); 
    } 
    else 
    { 
     control.Text = text; 
    } 
    } 
} 

使用簡單,但你必須定義更多的回調,更多的參數。

this.UpdateControlText(label1, "Hello world"); 
1

我認爲薩穆埃爾的(優秀)方法可以推進一步:

擴展方法:

public static void ExecuteAsync<TControl>(this TControl control, Action action) 
where TControl : Control 
{ 
    new Thread(() => 
    { 
    control.Invoke(action); 
    }) 
    .Start(); 
} 

表單代碼:

private void doStuff() 
{ 
    this.ExecuteAsync(() => 
    { 
    // Do your stuff in a separate thread 
    // but having full access to local or instance variables. 

    // No (visible) threading code needs to be used here. 
    }); 
} 
6

路加福音說,使用Control.I nvoke像這樣...

例如,在一種形式:

public delegate void DelegatePassMessages(string name, int value); 

public DelegatePassMessages passMessage; 

在構造器:

passMessage = new DelegatePassMessages (this.MessagesIn); 

然後MessagesIn函數來接收數據:

public void MessagesIn(string name, int value) 
{ 

} 

然後將數據傳遞給您的表單:

formName.Invoke(formName.passMessage, new Object[] { param1, param2}); 
0

找到一個優雅的.net 2.0方法,包含一個MethodInvoker委託中的匿名方法。這種方式無需一直定義自己的代表。例如:

private void InitUI(Guid id, string typename) 
    { 
     MethodInvoker inv = delegate{tvMatrix.Nodes[0].Nodes.Add(id.ToString(), typename);}; 
     tvMatrix.Invoke(inv); 
    } 
0

爲什麼不

tvMatrix.Invoke((MethodInvoker) (() => { 
    tvMatrix.Nodes[0].Nodes.Add(id.ToString(), typename); 
})); 
7

一些更多的可能性:

this.Invoke(new MethodInvoker(() => this.DoSomething(param1, param2))); 

this.Invoke(new Action(() => this.DoSomething(param1, param2))); 

甚至

this.Invoke(new Func<YourType>(() => this.DoSomething(param1, param2))); 

其中第一個選項是最好的選擇,因爲MethodInvoker是爲此目的而設計的,並且性能更好。

1

這裏ya去使用帶有Invoke()擴展+輸入參數的lambda表達式。

使用:動作(STARS分貝)

_ccb.GetImagerFRU_PartNbr().Invoke(new Action<STARS>(dbase => _ccb.GetImagerFRU_PartNbr().Text = dbase.PartNumber(serial) ?? String.Empty), db); 
0
private void ppTrace(string tv) 
    { 
     if (_Txb1.InvokeRequired) 
     { 
      _Txb1.Invoke((Action<string>)ppTrace, tv); 
     } 
     else 
     { 
      _Txb1.AppendText(tv + Environment.NewLine); 
     } 
    }