我有一個應用程序,其中一個線程用於計算一串數字,但問題是應用程序在線程完成計算並返回計算出的數字之前結束。在計算完成之前讓應用程序等待的方式是什麼?C#應用程序正在等待線程完成?
這裏是我的代碼:
namespace ThreadWaiting
{
class Program
{
public static int number=0;
public static void Main(string[] args)
{
Thread t = new Thread(Calculate);
t.Start();
//Wait here until processed?
Console.WriteLine(number);
Console.ReadKey();
}
private static void Calculate(object obj)
{
number = 2 + 2;
}
}
}
如果您運行,你會看到它打印出「0」,而不是「4」。
任何想法傢伙?謝謝!
線程同步FTW! – 2014-10-10 12:49:35
['Thread.Join'](http://msdn.microsoft.com/en-us/library/95hbf2ta(v = vs.110).aspx)就是你需要的所有東西 – DavidG 2014-10-10 12:49:50