线程间操作无效: 从不是创建控件“xxx”的线程访问它
2007-12-08 00:07
357 查看
线程间操作无效: 从不是创建控件“xxx”的线程访问它
2007年5月11日 1:55:32 发布:qiuyi
在WinForm中,直接使用自己创建的线程去使用窗体中的控件,是不安全的,不允许这样操作,以用委托的方式进行处理:delegate void WriteLabelText(string str, Color color);
private void showSqlConnResult(string msg, Color color)
{
this.labelSqlConnStatus.Text = msg;
this.labelSqlConnStatus.ForeColor = color;
this.buttonTestSqlConn.Enabled = true;
}
private void threadTestConn()
{
string result = "连接数据库成功。";
Color color = Color.Green;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(this.textBoxSqlConnString.Text.Trim());
try
{
conn.Open();
conn.Close();
}
catch (Exception E)
{
result = E.Message;
color = Color.Red;
}
WriteLabelText wt = new WriteLabelText(showSqlConnResult);
this.Invoke(wt, result, color);
}
private void buttonTestSqlConn_Click(object sender, EventArgs e)
{
this.buttonTestSqlConn.Enabled = false;
this.labelSqlConnStatus.ForeColor = Color.Blue;
this.labelSqlConnStatus.Text = "正在测试连接数据库……";
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.threadTestConn));
thread.Start();
相关文章推荐
- 后台线程与UI线程问题:解决“线程间操作无效: 从不是创建控件xxx的线程访问它”
- C#报错"线程间操作无效: 从不是创建控件“XXX”的线程访问它"--解决示例
- C# 引发“线程间操作无效: 从不是创建控件“XXX”的线程访问它” 解决方法
- 线程间操作无效: 从不是创建控件“XXX”的线程访问它。
- 线程间操作无效:从不是创建控件“XX”的线程访问它
- 线程间操作无效: 从不是创建控件的线程访问它 解决方案
- 线程间操作无效: 从不是创建控件“...”的线程访问它。
- 线程间操作无效: 从不是创建控件“Control Name'”的线程访问它问题的解决方案及原理分析
- 线程间操作无效:从不是创建控件“XX”的线程访问它,多线程问题
- 线程间操作无效,从不是创建控件的线程访问它。
- 线程间操作无效: 从不是创建控件“Control Name'”的线程访问它问题的解决方案及原理分析
- 【转】C#线程间操作无效: 从不是创建控件" XX" 的线程访问它
- 线程间操作无效: 从不是创建控件“”的线程访问它
- C# 线程间操作无效: 从不是创建控件的线程访问它
- “线程间操作无效: 从不是创建控件的线程访问它”
- 线程间操作无效: 从不是创建控件“...”的线程访问它。
- 线程间操作无效: 从不是创建控件"Form1"的线程访问它
- C#线程间操作无效: 从不是创建控件" XX" 的线程访问它
- 线程使用中常见的错误-“System.InvalidOperationException”线程间操作无效: 从不是创建控件“ ”的线程访问它。
- 线程间操作无效: 从不是创建控件“textBox1”的线程访问它。