threadroc | 浏览 1611 次
2013-07-15 09:33
既然开启了线程为何还要委托呢?,开启线程本身不就是委托吗?为何委托要显示声明呢?
Thread thread = new Thread(new ThreadStart(getAllRows));
thread.IsBackground = true;
thread.Start();
比如说以上这段代码,getAllRows就是一个普通方法,根本没用委托.
但是,我不太明白,这样也行,为何还要用委托,用委托的作用,好处在在哪里,这里不太懂,望解答.
2013-07-15 09:49
提问者采纳
用了线程,自然就不用委托了
委托最大的好处就是,它处理完以后会有回调事件,然后进行处理
而Thread一旦开启,就无法再控件他了
追问:
那我当初问的,登陆比如登陆100万个账号,取其信息采集,1个线程显然太慢了,所以要多线程,但是你当初说要用委托,是什么意思,不太懂... 望解惑,谢谢
追答:
委托也是一个线程。。。和Thread类似,但他可以有回调事件。。。仅此而已
追问:
啊,既然委托也是线程,不可以用Thread.CurrentThread.Abort()结束该委托启动的线程吗?
如果不能的话,委托怎样停止它自己启动的线程?
追答:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
namespace
ConsoleApplication2
{
class
Program
{
public
delegate
String TestMethod(String str);
static
void
Main(
string
[] args)
{
TestMethod d=
new
TestMethod(Hello);
d.BeginInvoke(
"Shiori"
,
delegate
(IAsyncResult ar)
{
if
(ar.IsCompleted)
{
TestMethod d = (TestMethod)ar.AsyncState;
String result = d.EndInvoke();
//Hello : Shiori
}
}, d);
}
static
String Hello(String yourName)
{
return
"hello : "
+ yourName;
}
}
}
|
异步委托的示例,你可以看看是怎么回事