C#为什么多线程控制winform需要用委托?
2010-02-04 23:51
274 查看
C#为什么多线程控制winform需要用委托?比如我新起了一个线程A,在A线程里要对winform的list控件里显示数据,我需要用一个委托函数来实现。
因为winform是主线程创建的,你用另外一个线程来调用它就可能会出现两个线程同时访问同一个资源的问题,这个时候很容易出现错误,比如A线程改变窗口的颜色为红色,B线程取窗口的颜色,如果这两个线程正好碰到一起,可能A略先于B,那么就是B取出来的红色,如果略后于那么就是原来的颜色,而这和CPU的繁忙度、时间片的轮转是相关的,是一种随机的情况,那么B取出来的颜色就不可靠了,因此为了避免这种状况采用委托,B线程向A线程发出委托,由A线程来完成取色工作,那么可以保证取色工作的稳定性,结果也可靠
如果你实在想通过另一个线程访问winform,可以在窗口的构造函数中加入
Control.CheckForIllegalCrossThreadCalls = false;
这样可以屏蔽这个错误,不过建议还是用委托
这需要用到委托.
你先声明一个委托,然后,把一个操作界面的函数托给他.
你的线程里只要调用这个委托就可以了.
C#试写一个多线程问题(委托,Invoke(),beginInvoke())
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace _
{
public partial class Form1 : Form
{
public delegate void del(string str);
public delegate void del1(string str1);
Thread thread1,thread2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.thread1 = new Thread(new ThreadStart(this.sa));
this.thread1.Start();
//MessageBox.Show(this,);
this.button1.Enabled = false;
//获取当前的线程好
int thread1id = Thread.CurrentThread.GetHashCode();
string strThreadId = Convert.ToString(thread1id);
MessageBox.Show(this,strThreadId);
}
private void progressSet(string text)
{
progressBar1.Value = 0;
progressBar1.Maximum = 999999;
for (int i = 0; i < 999999; i++)
{
progressBar1.Value++;
}
progressBar1.Value = 0;
}
private void sa()
{
del pro = new del(progressSet);
this.Invoke(pro, new object[] { "hello" });
}
private void button2_Click(object sender, EventArgs e)
{
this.thread2 = new Thread(this.sa1);
thread2.Start();
this.button2.Enabled = false;
//获取当前的线程好
int thread2id = Thread.CurrentThread.GetHashCode();
string strThreadId = Convert.ToString(thread2id);
MessageBox.Show(this, strThreadId);
}
private void sa1()
{
//其他操作
//比如将界面的TextBox内容设置一下
del1 mi1 = new del1(UpdateTextBox);
this.Invoke(mi1, new object[] { "我是一个文本框" });
}
//更新界面的方法
private void UpdateTextBox(string str)
{
//更新
this.textBox1.Text = str;
}
}
}
因为winform是主线程创建的,你用另外一个线程来调用它就可能会出现两个线程同时访问同一个资源的问题,这个时候很容易出现错误,比如A线程改变窗口的颜色为红色,B线程取窗口的颜色,如果这两个线程正好碰到一起,可能A略先于B,那么就是B取出来的红色,如果略后于那么就是原来的颜色,而这和CPU的繁忙度、时间片的轮转是相关的,是一种随机的情况,那么B取出来的颜色就不可靠了,因此为了避免这种状况采用委托,B线程向A线程发出委托,由A线程来完成取色工作,那么可以保证取色工作的稳定性,结果也可靠
如果你实在想通过另一个线程访问winform,可以在窗口的构造函数中加入
Control.CheckForIllegalCrossThreadCalls = false;
这样可以屏蔽这个错误,不过建议还是用委托
这需要用到委托.
你先声明一个委托,然后,把一个操作界面的函数托给他.
你的线程里只要调用这个委托就可以了.
C#试写一个多线程问题(委托,Invoke(),beginInvoke())
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace _
{
public partial class Form1 : Form
{
public delegate void del(string str);
public delegate void del1(string str1);
Thread thread1,thread2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.thread1 = new Thread(new ThreadStart(this.sa));
this.thread1.Start();
//MessageBox.Show(this,);
this.button1.Enabled = false;
//获取当前的线程好
int thread1id = Thread.CurrentThread.GetHashCode();
string strThreadId = Convert.ToString(thread1id);
MessageBox.Show(this,strThreadId);
}
private void progressSet(string text)
{
progressBar1.Value = 0;
progressBar1.Maximum = 999999;
for (int i = 0; i < 999999; i++)
{
progressBar1.Value++;
}
progressBar1.Value = 0;
}
private void sa()
{
del pro = new del(progressSet);
this.Invoke(pro, new object[] { "hello" });
}
private void button2_Click(object sender, EventArgs e)
{
this.thread2 = new Thread(this.sa1);
thread2.Start();
this.button2.Enabled = false;
//获取当前的线程好
int thread2id = Thread.CurrentThread.GetHashCode();
string strThreadId = Convert.ToString(thread2id);
MessageBox.Show(this, strThreadId);
}
private void sa1()
{
//其他操作
//比如将界面的TextBox内容设置一下
del1 mi1 = new del1(UpdateTextBox);
this.Invoke(mi1, new object[] { "我是一个文本框" });
}
//更新界面的方法
private void UpdateTextBox(string str)
{
//更新
this.textBox1.Text = str;
}
}
}
相关文章推荐
- C#为什么多线程控制winform需要用委托?
- C#为什么多线程控制winform需要用委托?
- C#为什么多线程控制winform需要用委托?
- 转载:C#为什么多线程控制winform需要用委托?
- !!c# 委托与多线程 -- 相比background来说,本方法针对当需要通过异步代理发起UI操作,而后线程中进行sleep操作的时使用
- C# 通过委托控制进度条以及多线程更新控件
- c#中跨线程调用windows窗体控件 .我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。
- C# 多线程控制控件实例(invoke,控件的委托方法)
- C# 通过委托控制进度条以及多线程更新控件
- C# 通过委托控制进度条以及多线程更新控件
- c#为什么需要事件委托
- 非阻塞IO、NIO为什么会快,我们为什么需要多线程
- C# 多线程编程 - 使用委托创建线程
- C#的委托事件在winform窗体中实现传值备忘
- C# 多线程、控制线程数提高循环输出效率
- 两张图说明C#Winform为什么没有[design]界面
- c# 委托与多线程
- C# winform编程中多线程操作控件方法
- [C# 基础知识系列]专题一:深入解析委托——C#中为什么要引入委托
- C#中使用多线程访问Winform问题解决方案