来自:http://apps.hi.baidu.com/share/detail/20133509
提起多线程,不得不提起 委托(delegates)这个概念.
我理解的委托就是 具有 同样参数和返回值 的函数的集合.
比如
public delegate void MyDelegate(int arg);
就是这种形式的函数 void Myfuntion(int i); 的集合.
如何将一个函数加入 委托 的集合?
MyDelegate dele = new MyDelegate(Myfuntion1);
再增加一个
dele += new MyDelegate(Myfuntion2);
...
委托函数 dele 就是 具有整数参数和空返回值的函数 Myfuntion1,2的集合.
调用这个委托函数
dele(1);
就是逐个调用 Myfuntion1,2,...
一般线程函数的声明和启动
Thread t = new Thread(new ThreadStart(MyFunction));
t.Start();
正是调用了没有参数和返回值的 委托函数 ThreadStart
其中的参数MyFunction 是 这个委托函数中的一员.
很明显 这样无法传参数和返回值,那我们该怎么办?
答案就在委托 的BeginInvoke() 方法上, BeginInvoke() 也是(异步)启动一个新线程.
例如
MyDelegate dele = new MyDelegate (MyFunction);
dele.BeginInvoke(10,"abcd");
void MyFunction(int count, string str);
可以实现参数的传递.
如何收集线程函数 的 返回值?
与 BeginInvoke 对应 有个 EndInvoke 方法,而且运行完毕返回 IAsyncResult 类型的返回值.
这样我们可以这样收集 线程函数的 返回值
MyDelegate dele = new MyDelegate (MyFunction);
IAsyncResult ref = dele.BeginInvoke(10,"abcd");
...
int result = dele.EndInvoke(ref); <----收集 返回值
int MyFunction(int count, string str); <----带参数和返回值的 线程函数
提示:"线程间操作无效:从不是创建控件“XX”的线程访问它"
一般来说,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能简单的通过控件对象名来操作,但不是说不能进行操作,微软提供了Invoke的方法,其作用就是让子线程告诉窗体线程来完成相应的控件操作。 现在用一个用线程控制的进程条来说明,大致的步骤如下:
1. 创建Invoke函数,大致如下: ///
/// Delegate function to be invoked by main thread ///
private void InvokeFun()
{ if( prgBar.Value < 100 )
prgBar.Value = prgBar.Value + 1; }
2. 子线程入口函数: ///
/// Thread function interface ///
private void ThreadFun()
{ //Create invoke method by specific function
MethodInvoker mi = new MethodInvoker( this.InvokeFun );
for( int i = 0; i < 100; i++ ) { this.BeginInvoke( mi ); Thread.Sleep( 100 ); } }
3. 创建子线程:
Thread thdProcess = new Thread( new ThreadStart( ThreadFun ) );
thdProcess.Start();
出现这个问题主要是因为在线程方法中操作了界面上的控件..lstPrime.Items.Add()
可以这样改下..
//定义一个委托
public delegate void MyInvoke(string str);
//定义一个操作界面的方法
private void UpdateUI(string str)
{
//增加项
this.lstPrime.Items.Add(str);
}
//在线程的方法中,即你的Generate方法..
//里面只要是涉及到Items.Add操作的都改成如下形式即可..
//比如lstPrime.Items.Add(2);改成:
MyInvoke mi=new MyInvoke(UpdateUI);
this.BeginInvoke(mi,new object[]{ "2 "});
============================================================================
BeginInvoke与Invoke的含义[转载]BeginInvoke 方法真的是新开一个线程进行异步调用吗?
参考以下代码: