Winform软件,不要在线程里操作UI
2016-11-13 21:02
507 查看
对于Winform软件,不要在线程里操作UI,不要相信:StartForm.CheckForIllegalCrossThreadCalls = false;
于是,把所有的代码都改成主线程委托调用的方式
2
3
Use it like this:
3[/code]
4
调用:
更新为:
考虑仍出现异常时:
5[/code]
变更为:
调用方法:
于是,把所有的代码都改成主线程委托调用的方式
private delegate void SetTextHandle(string id, string value); private void ThreadSetText(string id, string value) { this.Controls.Find(id, true)[0].Text = value; } private void SetText(string id, string value) { if (this.InvokeRequired) { this.Invoke(new SetTextHandle(ThreadSetText), new object[] { id, value }); } else { ThreadSetText(id, value); } }
2
// the canonical form (C# consumer) public delegate void ControlStringConsumer(Control control, string text); // defines a delegate type public void SetText(Control control, string text) { if (control.InvokeRequired) { control.Invoke(new ControlStringConsumer(SetText), new object[]{control, text}); // invoking itself } else { control.Text=text; // the "functional part", executing only on the main thread } }
3
public static class ControlHelpers { public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : ISynchronizeInvoke { if (control.InvokeRequired) { control.Invoke(new Action(() => action(control)), null); } else { action(control); } } }
Use it like this:
private void UpdateSummary(string text) { summary.InvokeIfRequired(s => { s.Text = text }); }
3[/code]
theLabel.Invoke(new Action(() => theLabel.Text = "hello world from worker thread!"));
4
public static void InvokeIfRequired(this Control control, MethodInvoker action) { if (control.InvokeRequired) { control.Invoke(action); } else { action(); } }
调用:
richEditControl1.InvokeIfRequired(() => { // Do anything you want with the control here richEditControl1.RtfText = value; RtfHelpers.AddMissingStyles(richEditControl1); });
更新为:
public static void InvokeIfRequired(this ISynchronizeInvoke obj, MethodInvoker action) { if (obj.InvokeRequired) { var args = new object[0]; obj.Invoke(action, args); } else { action(); } }
考虑仍出现异常时:
while (!control.Visible) { System.Threading.Thread.Sleep(50); }
5[/code]
public static void InvokeIfRequired(this Control c, Action<Control> action) { if(c.InvokeRequired) { c.Invoke(new Action(() => action(c))); } else { action(c); } }
变更为:
public static void InvokeIfRequired<T>(this T c, Action<T> action) where T : Control
调用方法:
object1.InvokeIfRequired(c => { c.Visible = true; });
相关文章推荐
- winform线程间操作UI的五种方法
- [WinForm]WinForm跨线程UI操作常用控件类大全
- android UI 操作 不要在子线程中操作UI
- [WinForm]WinForm跨线程UI操作常用控件类大全
- 关于在非UI线程中进行UI操作会出现问题: Can't create handler inside thread that has not called Looper.prepare()
- Winform跨线程操作最简单的办法
- winform2.0中解决跨线程操作控件的方法总结
- winform 多线程执行操作,并跨线程操作控件
- WinForm中线程问题:线程间操作无效: 从不是创建控件“...”的线程访问它
- winform应用程序:非控件创建线程无法操作控件
- 使用Activity类的runOnUiThread执行线程更新UI操作
- Winform之UI后台线程
- 基于接口和队列的线程操作UI方式
- Winform 线程修改UI界面元素的值
- 分析SpinnerActivityTest中有关控件操作以及UI线程问题
- Winform中多线程的控件访问引发的“线程间操作无效,从不是创建控件的线程访问它”
- 为什么Winform不允许跨线程修改UI线程控件的值?
- [VB.NET]在线程里频繁的进行委托操作主界面的ui是不是效果不好?
- RCP 非UI线程调用UI操作
- WinForm中线程的处理(UI线程)