我有以下代码示例:
class Program
{
static void Main(string[] args)
{
var a = true;
Task t = Task.Factory.StartNew(() =>
{
a = getBoolean();
});
Task.Factory.StartNew(() =>
{
Console.ReadLine();
});
while (!t.IsCompleted)
{
//Console.WriteLine("initial : a = {0}", a);
}
if (a == true)
{
Console.WriteLine("From system a = {0}", true);
}
else
{
Console.WriteLine("From system a = {0}", false);
}
}
public static bool getBoolean()
{
Task<bool> a = DelayTask();
bool r = a.Result;
a.Dispose();
return r;
}
public static Task<bool> DelayTask()
{
return Task.Factory.StartNew<bool>(() =>
{
Thread.Sleep(30000);
return false;
});
}
}
一旦我在我的窗口上方运行此代码,就会自动死机。(我必须按笔记本电脑上的电源按钮)。有人可以帮我重现这个问题并解释为什么会发生这种情况。