1.使用Thread
2.使用Task 据说是.NET4.0加入的
private void Form2_Load(object sender, EventArgs e){
//Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());
textBox1.Text = "1";
Task<string> task = new Task<string>(run);//可以在这里传参
task.Start();
bool b = task.IsCompleted;
Console.WriteLine(b);
var waiter=task.GetAwaiter();
waiter.OnCompleted(() => { Console.WriteLine(task.Result);}); 相当于回调了
//var r=task.Result; 会阻塞主线程
//textBox1.Text = r;
Console.WriteLine(098776554);
}
private string run()
{
Thread.Sleep(2000);
return Thread.CurrentThread.ManagedThreadId.ToString();
}
3.