线程间操作无效: 从不是创建控件的线程访问它的三种方法
来源:互联网 发布:mac os x能玩剑网三吗? 编辑:程序博客网 时间:2024/05/22 01:38
看下解决办法吧
1、把CheckForIllegalCrossThreadCalls设置为false
2、利用委托
delegate void SetTextCallBack(string text); private void SetText(string text) { if (this.txt_a.InvokeRequired) { SetTextCallBack stcb = new SetTextCallBack(SetText); this.Invoke(stcb , new object[] { text}); } else { this.txt_a.Text = text; } } private void LoadData() { SetText("测试"); } //窗体加载时,用线程加载数据 private void Frm_ImportManager_Load(object sender, EventArgs e) { ThreadStart ts = new ThreadStart(LoadData); Thread thread = new Thread(ts); thread.Name = "LoadData"; thread.Start(); }
3、使用 BackgroundWorker控件
在应用程序中实现多线程的首选方式是使用 BackgroundWorker 组件。BackgroundWorker 组件使用事件驱动模型实现多线程。辅助线程运行 DoWork 事件处理程序,创建控件的线程运行 ProgressChanged 和 RunWorkerCompleted 事件处理程序。注意不要从 DoWork 事件处理程序调用您的任何控件。
下面的代码示例不异步执行任何工作,因此没有 DoWork 事件处理程序的实现。TextBox 控件的 Text 属性在 RunWorkerCompleted 事件处理程序中直接设置。
// This event handler starts the form's // BackgroundWorker by calling RunWorkerAsync. // // The Text property of the TextBox control is set // when the BackgroundWorker raises the RunWorkerCompleted // event. private void setTextBackgroundWorkerBtn_Click( object sender, EventArgs e) { this.backgroundWorker1.RunWorkerAsync(); } // This event handler sets the Text property of the TextBox // control. It is called on the thread that created the // TextBox control, so the call is thread-safe. // // BackgroundWorker is the preferred way to perform asynchronous // operations. private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { this.textBox1.Text = "This text was set safely by BackgroundWorker."; }
0 0
- 线程间操作无效: 从不是创建控件的线程访问它的三种方法
- 线程间操作无效:从不是创建控件的线程访问它的三种方法
- 线程间操作无效: 从不是创建控件的线程访问它的三种解决方法
- 线程间操作无效: 从不是创建控件的线程访问它的两种方法
- WinForm中线程问题:线程间操作无效: 从不是创建控件“...”的线程访问它
- c#线程问题:线程间操作无效:从不是创建控件 的线程访问它
- WinForm中线程问题:线程间操作无效: 从不是创建控件“...”的线程访问它
- “线程间操作无效:从不是创建控件“XX”的线程访问它”的解决方法
- 线程间操作无效: 从不是创建控件“”的线程访问它的解决方法
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法!
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 线程间操作无效 从不是创建控件“___”的线程访问它”解决方法的疑惑
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法
- C# 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 动态调整控件现实隐藏
- 定义字典
- Hibernate使用总结。
- React Native通信机制详解【转】
- 综合:Android安装提示 INSTALL_FAILED_INSUFFICIENT_STORAGE
- 线程间操作无效: 从不是创建控件的线程访问它的三种方法
- 国内常见的一些代码托管平台
- tomcat配置文件server.xml详解
- ODBCHelper.cs
- 怎样读取一个文件夹下的头文件名,并整理出一个新的头文件
- 关于超时的数据库设置
- Optimized Gnome sort: Sorting by Exchanging
- android整合小米、个推、jpush推送
- 猜想一下未来ipv6网络可能的新架构--将颠覆现在的即时通讯中必须使用长连接的现状