c#多线程更新窗口(winform)GUI的数据
2014-08-11 00:00
330 查看
1. 在.net framwork 2.0中,可以通过以下代码来实现:
调用方式如下:
2.在.net 3.0或者更新的版本中,你可以重写上面的方法作为一个Control类的扩展方法,可以简化调用方式,具体代码如下:
在.net 3.0以上的版本完整的调用步骤如下:
通过使用LINQ和lambda表达式使代码更加简洁:
3.最简单的匿名方法调用:
以上方法搜集自网络,仅供参考。
转载请注明:文章转载自:[169IT-最新最全的IT资讯]
本文标题:c#多线程更新窗口(winform)GUI的数据
1 2 3 4 5 6 7 8 9 10 11 12 | private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue); public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue) { if (control.InvokeRequired) { control.Invoke( new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object [] { control,propertyName,propertyValue }); } else { control.GetType().InvokeMember(propertyName,BindingFlags.SetProperty, null ,control, new object [] { propertyValue }); } } |
1 2 3 | // thread-safe equivalent of // myLabel.Text = status; SetControlPropertyThreadSafe(myLabel, "Text" ,status); |
1 | myLabel.SetPropertyThreadSafe( "Text" ,status); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | private delegate void SetPropertyThreadSafeDelegate<TResult>(Control @ this ,Expression<Func<TResult>>property,TResult value); public static void SetPropertyThreadSafe<TResult>( this Control @ this ,Expression<Func<TResult>>property,TResult value) { var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo; if (propertyInfo == null || !@ this .GetType().IsSubclassOf(propertyInfo.ReflectedType) || @ this .GetType().GetProperty(propertyInfo.Name,propertyInfo.PropertyType) == null ) { throw new ArgumentException( "The lambda expression 'property' must reference a valid property on this Control." ); } if (@ this .InvokeRequired) { @ this .Invoke( new SetPropertyThreadSafeDelegate<TResult>(SetPropertyThreadSafe), new object [] { @ this ,property,value }); } else { @ this .GetType().InvokeMember(propertyInfo.Name,BindingFlags.SetProperty, null ,@ this , new object [] { value }); } } |
1 | myLabel.SetPropertyThreadSafe(() =>myLabel.Text,status); // status has to be a string or this will fail to compile |
1 2 3 4 5 6 | ///...blah blah updating files string newText = "abc" ; // running on worker thread this .Invoke((MethodInvoker) delegate { someLabel.Text = newText; // runs on UI thread }); ///...blah blah more updating files |
转载请注明:文章转载自:[169IT-最新最全的IT资讯]
本文标题:c#多线程更新窗口(winform)GUI的数据
相关文章推荐
- 解决列表框更新数据的时候的闪烁问题(VC防止窗口及其控件(如CListCtrl)闪烁的简单方法(一组有用的宏) )
- 数据窗口中有三种更新方式选择
- 如何在PB数据窗口中设置数据窗口的更新属性
- WPF、WinForm(C#)多线程编程并更新界面(UI)
- 多线程之winform界面的更新
- WinForm下TextBox的数据绑定和更新
- 数据窗口的多表更新
- Android多线程中使用Handler更新ListView控件数据
- c#.net2005向sql添加、更新、删除数据的原代码(winform)
- Winform开发框架之数据即时更新的实现
- 实现即时更新数据 反映在DataView或则DataGrid 在winform ·····
- 委托之winForm窗口间传递数据
- 实现PowerBuilder数据窗口的多表更新
- Winform使用ADO.NET的SqlDataAdapter更新多表数据
- winform 数据绑定 对象的属性,后台异步更新此对象的属性产生异常
- 数据窗口的多表更新
- 在vc6线程中如何更新窗口数据
- winform两个窗口传参数,并刷新数据
- Winform开发框架之数据即时更新的实现
- Winform数据即时更新 父窗体子窗体即时更新