考虑代码:
class Work
{
public void DoStuff(string s)
{
Console.WriteLine(s);
// .. whatever
}
}
class Master
{
private readonly Work work = new Work();
public void Execute()
{
string hello = "hello";
// (1) is this an ugly hack ?
var thread1 = new Thread(new ParameterizedThreadStart(o => this.work.DoStuff((string)o)));
thread1.Start(hello);
thread1.Join();
// (2) is this similar to the one above?
new Action<string>(s => this.work.DoStuff(s)).BeginInvoke(hello, null, null);
}
}
(1) 在单独的线程中轻松开始某些工作是一种可接受的方式吗?如果不是更好的选择,将不胜感激。
(2) 做同样的事情吗?我想我要问的是是否启动了一个新线程,或者..
希望你能帮助初学者更好地理解:)
/莫伯格