從我在C#中找到的,Control.Invoke方法要求您使用沒有輸入參數的委託。有沒有辦法解決?我想調用一個方法來從另一個線程更新UI並傳遞給它的字符串參數。Control.Invoke與輸入參數
12
A
回答
22
您正在使用哪種版本的C#?如果您使用的是C#3.5,則可以使用閉包來避免傳入參數。
用C#3.5public 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);
}
}
相關問題
- 1. Control.Invoke()與IProgress/ReportProgress
- 2. 輸入輸出參數與不帶副作用的參數
- 3. 與Control.invoke一起使用Webbrowser
- 4. 與輸出參數
- 5. BLAKE2輸入參數
- 6. 輸入和參數
- 7. Win32com參數輸入
- 8. Control.Invoke()stucks
- 9. MATLAB函數輸入參數
- 10. 與輸入輸入數據[]在數學
- 11. JDBC執行與返回值和輸入/輸出參數
- 12. VBScript「類型不匹配」問題與「[輸入,輸出] BSTR *」參數
- 13. Perl的輸入參數
- 14. Teradata的REGEXP_SPLIT_TO_TABLE輸入參數
- 15. Z3_benchmark_to_smtlib_string()的輸入參數
- 16. 移位輸入參數
- 17. xslt中的輸入參數
- 18. HTTP URL輸入參數
- 19. FFMPEG的輸入參數
- 20. 使用輸入參數
- 21. DDD庫輸入參數
- 22. 線程輸入參數
- 23. Ofstream拒絕輸入參數
- 24. EmguCV FindFundamentalMat - 輸入參數
- 25. Control.Invoke與具有TaskScheduler的任務
- 26. control.invoke()問題
- 27. ctypes的輸入和輸出參數
- 28. 在WF4中輸入輸出參數
- 29. 更改過程輸入輸出參數
- 30. WebServices中的輸入和輸出參數