异常:“在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。”

vm58 2003-08-25 03:48:16
如何解决?
...全文
724 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
alaisalaix 2003-08-26
  • 打赏
  • 举报
回复
(转载)
The following example demonstrates how to create a background thread that uses a MethodInvoker to update a ProgressBar control at regular intervals:

namespace Microsoft.Samples.WinForms.Cs.ThreadMarshal {
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;


public class ThreadMarshal : System.Windows.Forms.Form {
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1;

private Thread timerThread;

public ThreadMarshal() {

// Required by the Windows Forms Designer
InitializeComponent();

}

//This function is executed on a background thread - it marshalls calls to
//update the UI back to the foreground thread
public void ThreadProc() {

try {
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
while (true) {
//Call BeginInvoke on the Form
this.BeginInvoke(mi);
Thread.Sleep(500) ;
}
}
//Thrown when the thread is interupted by the main thread - exiting the loop
catch (ThreadInterruptedException) {
//Simply exit....
}
catch (Exception) {
}
}

//This function is called from the background thread
private void UpdateProgress() {

//Reset to start if required
if (progressBar1.Value == progressBar1.Maximum) {
progressBar1.Value = progressBar1.Minimum ;
}

progressBar1.PerformStep() ;
}

//Start the background thread to update the progress bar
private void button1_Click(object sender, System.EventArgs e) {
StopThread();
timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
timerThread.Start();
}

//Stop the background thread to update the progress bar
private void button2_Click(object sender, System.EventArgs e) {
StopThread();
}

//Stop the background thread
private void StopThread()
{
if (timerThread != null)
{
timerThread.Interrupt();
timerThread = null;
}
}

protected override void Dispose(bool disposing)
{
StopThread();
if (disposing) {
if (components != null) {
components.Dispose();
}
}

base.Dispose(disposing);
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button1.Font = new System.Drawing.Font("Tahoma", 8, System.Drawing.FontStyle.Bold);
this.button1.Location = new System.Drawing.Point(128, 64);
this.button1.Size = new System.Drawing.Size(120, 40);
this.button1.TabIndex = 1;
this.button1.Text = "Start!";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Font = new System.Drawing.Font("Tahoma", 8, System.Drawing.FontStyle.Bold);
this.button2.Location = new System.Drawing.Point(256, 64);
this.button2.Size = new System.Drawing.Size(120, 40);
this.button2.TabIndex = 1;
this.button2.Text = "Stop!";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.progressBar1.Font = new System.Drawing.Font("Tahoma", 8, System.Drawing.FontStyle.Bold);
this.progressBar1.Location = new System.Drawing.Point(10, 10);
this.progressBar1.Size = new System.Drawing.Size(350, 40);
this.progressBar1.TabIndex = 2;
this.progressBar1.Text = "Start!";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 117);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1,
this.button2,
this.progressBar1});
this.Text = "Built using the Designer";

}

[STAThread]
public static void Main(string[] args) {
Application.Run(new ThreadMarshal());
}


}
}


Windows Forms controls can only execute on the thread on which they were created, that is, they are not thread-safe. If you want to get or set properties, or call methods, on a control from a background thread, the call must be marshaled to the thread that created the control.

There are five functions on a control that are safe to call from any thread: InvokeRequired, Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of the invoke methods.

By default, Windows marshals the calls for you. However, if you are making multiple calls to a control, it is much more efficient to create a method that executes those calls and make the one cross-thread call yourself. You make the cross-thread call by calling one of the Control.Invoke methods. The Invoke methods take a reference to a delegate. Typically, this delegate is an instance of the MethodInvoker delegate.
saucer 2003-08-26
  • 打赏
  • 举报
回复
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q318607

"..................
By design, Windows Form or Control methods cannot be called on a thread other than the one that created the form or control. If you attempt to do this, an exception is thrown. Depending on the exception handling implemented in your code, this exception may cause your application to terminate. If no exception handling is implemented, the following error message is displayed:

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Controls created on one thread cannot be parented to a control on a different thread.

The exception is raised because Windows Forms are based on a single-threaded apartment (STA) model. Windows Forms can be created on any thread; after they are created, however, they cannot be switched to a different thread. In addition, the Windows Form methods cannot be accessed on another thread; this means that all method calls must be executed on the thread that created the form or control.

Method calls that originate outside the creation thread must be marshalled (executed) on the creation thread. To do this asynchronously, the form has a BeginInvoke method that forces the method to be executed on the thread that created the form or control. The synchronous method call is done with a call to the Invoke method.
..................."
xfqiu 2003-08-25
  • 打赏
  • 举报
回复
这问题我原来碰到过.是在多线程应用的时候,应特别注意变量的适用范围,在一个线程(非主线程)内new的一个变量,在另一个线程内是无法使用的.
目 录 第一章 开发环境 1.1 Qt 简介5 1.2 下载安装 Qt Creator 6 1.3 第一个程序 Hello World 7 第二章 窗体应用 1.1 窗体基类说明 12 1.2 控制窗体大小 13 1.3 窗体初始位置及背景色 13 1.4 修改标题栏图标 14 1.5 移动无边框窗体 16 1.6 去掉标题栏中最大化、最小化按钮 17 1.7 多窗体调用 18 1.8 字体形状窗体 20 第三章 控件应用 1.1 QPushButton按钮 23 1.2 QLabel标签 23 1.3 QLineEdit单行文本 24 1.4 QTextEdit多行文本 25 1.5 QPlainTextEdit多行文本 26 1.6 QComboBox下拉列表框 26 1.7 QFontComboBox字体下拉列表框 27 1.8 QSpinBox控件 28 1.9 QTimeEdit时间控件 29 1.10 QDateEdit日期控件 30 1.11 QScrollBar控件 30 1.12 QRadioButton单选按钮 31 1.13 QCheckBox复选框 32 1.14 QListView 列表控件 34 1.15 QTreeView树控件 34 1.16 QTableView表格控件 35 1.17 QHBoxLayout横向布局 36 1.18 QGridLayout网格布局 37 1.19 QGroupBox控件 38 1.20 QTabWidget控件 39 1.21 QMenu、QToolBar控件 41 1.22 任务栏托盘菜单 43 第四章 组件应用 1.1日历组件 47 1.2登录窗口 48 1.3文件浏览对话框 50 1.4颜色选择对话框 51 1.5进度条实例53 1.6Timer实时更新时间 54 第五章 文件操作 1.1创建文件夹 57 1.2写入文件 58 1.3修改文件内容 60 1.4删除文件 62 1.5修改文件名 63 1.6 INI文件写入操作 65 1.7 INI文件读取操作 68 1.8创建XML文件 71 1.9读取XML文件 72 第六章 图形图像操作 1.1绘制文字 75 1.2绘制线条 75 1.3绘制椭圆 77 1.4显示静态图像 78 1.5显示动态图像 78 1.6图片水平移动 79 1.7图片翻转 80 1.8图片缩放 82 1.9图片中加文字 84 1.10图像扭曲 85 1.11模糊效果 85 1.12着色效果 86 1.13阴影效果 87 1.14透明效果 87 第七章 多媒体应用 1.1音频、视频播放器 90 1.2播放Flash动画 94 1.3播放图片动画 95 第八章 系统操作 1.1获取屏幕分辨率 98 1.2获取本机名、IP地址 98 1.3根据网址获取IP地址 99 1.4判断键盘按下键值 100 1.5获取系统环境变量 101 1.6执行系统命令 102 第九章 注册表 1.0简要说明注册表 105 1.1写入注册表 105 1.2查找注册表 106 1.3修改IE浏览器的默认主页 107 第十章 数据库基础 1.1查询数据库驱动 109 1.2Qodbc连接Access数据库 109 1.3插入数据 111 1.4数据列表 112 1.5操作SQLite数据库 113 1.6SQLite数据库视图管理器 115 第十一章 网络开发 1.1点对点聊天服务端 119 1.2点对点聊天客户端 123 1.3局域网广播聊天 128 1.4SMTP协议发送邮件 148 1.5调用系统DLL判断网络连接状态 152 第十二章 进程与线程 1.1进程管理器 155 1.2线程QThread应用 158 1.3线程QRunnable应用 159 第十三章 数据安全 1.1 QByteArray加密数据 163 1.2 AES加密数据 164 1.3 MD5 加密数据 165 1.4 生成随机数 166 第十四章 打包部署 1.1 FilePacker 打包 169 1.2 Inno Setup 打包 174

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧