线程间操作无效: 从不是创建控件“progressBar1”的线程访问它
2015-03-22 12:41
309 查看
http://wangfeng5271.blog.163.com/blog/static/48174444201191910133512/
在窗体 load 加入
private void MailDesktop_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
还找了一些其他方法~~
2、创建代理
delegate void SetTextCallback(string text);
创建和启动线程
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcUnsafe));
this.demoThread.Start();
线程中要求改主窗体UI中的text属性
private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
}
调用窗体中的函数用invoke传递参数
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
3、
{
1.定义 委托
delegate void myDelegate(int i);
myDelegate mydelegate = null;
2.定义方法,显示消息
public void ShowMessage(int i)
{
this.textBox1.Text = i.ToString();
this.progressBar1.Value = i;
}
3.定义方法,驱动消息
public void MyEvent()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
this.BeginInvoke(mydelegate, new object[] {i});
}
}
4: 运行
private void button1_Click(object sender, EventArgs e)
{
mydelegate = new myDelegate(ShowMessage);
Thread myThread = new Thread(MyEvent);
//IsBackground 是否后台
//这个属性很重要 .如果 Thread IsBackground 等于false
// 当线程还没有结束时,你点了关闭按钮
// 将抛出An unhandled exception
//of type 'System.InvalidOperationException'
//occurred in System.Windows.Forms.dll 异常
myThread.IsBackground = true;
myThread.Start();
}
}
在窗体 load 加入
private void MailDesktop_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
还找了一些其他方法~~
2、创建代理
delegate void SetTextCallback(string text);
创建和启动线程
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcUnsafe));
this.demoThread.Start();
线程中要求改主窗体UI中的text属性
private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
}
调用窗体中的函数用invoke传递参数
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
3、
{
1.定义 委托
delegate void myDelegate(int i);
myDelegate mydelegate = null;
2.定义方法,显示消息
public void ShowMessage(int i)
{
this.textBox1.Text = i.ToString();
this.progressBar1.Value = i;
}
3.定义方法,驱动消息
public void MyEvent()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
this.BeginInvoke(mydelegate, new object[] {i});
}
}
4: 运行
private void button1_Click(object sender, EventArgs e)
{
mydelegate = new myDelegate(ShowMessage);
Thread myThread = new Thread(MyEvent);
//IsBackground 是否后台
//这个属性很重要 .如果 Thread IsBackground 等于false
// 当线程还没有结束时,你点了关闭按钮
// 将抛出An unhandled exception
//of type 'System.InvalidOperationException'
//occurred in System.Windows.Forms.dll 异常
myThread.IsBackground = true;
myThread.Start();
}
}
相关文章推荐
- 线程间操作无效: 从不是创建控件“progressBar1”的线程访问它。
- 线程间操作无效: 从不是创建控件“”的线程访问它
- 线程间操作无效:从不是创建控件“XX”的线程访问它
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 线程间操作无效: 从不是创建控件的线程访问它
- 线程间操作无效: 从不是创建控件“...”的线程访问它。
- 线程间操作无效: 从不是创建控件“xxx”的线程访问它
- 线程间操作无效,从不是创建控件的线程访问控件的解决方案
- BackGroundWorker解决“线程间操作无效: 从不是创建控件的线程访问它”
- 教你如何解决“线程间操作无效: 从不是创建控件的线程访问它”
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 线程间操作无效:从不是创建控件“XX”的线程访问它
- 教你如何解决“线程间操作无效: 从不是创建控件的线程访问它”
- 线程间操作无效:从不是创建控件“XX”的线程访问它
- 线程间操作无效: 从不是创建控件 的线程访问它
- 如何解决“线程间操作无效: 从不是创建控件的线程访问它”
- 线程间操作无效:从不是创建控件“txtTest”的线程访问它
- BackGroundWorker解决“线程间操作无效: 从不是创建控件的线程访问它”
- 线程间操作无效:从不是创建控件 "xxxx" 的线程访问它。