2
我想通過局部變量獲得線程結果。如何通過局部變量獲取線程結果?
有代碼:
static void Main()
{
long res1 = 0, res2 = 0;
long n1 = 5000, n2 = 10000;
Thread t1 = new Thread(() =>
{
res1 = Factorial(n1);
});
Thread t2 = new Thread(() => { res2=Factorial(n2); });
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine("Factorial of {0} equals {1}", n1, res1);
Console.WriteLine("Factorial of {0} equals {1}", n2, res2);
}
輸出:
Factorial of 5000 equals 0
Factorial of 10000 equals 0
爲什麼這段代碼返回0?
這是階乘函數:
static long Factorial(long n)
{
long res = 1;
do
{
res = res * n;
} while(--n > 0);
return res;
}
隨着'Task'得到的結果是更容易。你不需要自己編組結果。 – usr 2014-10-30 12:19:04