【发布时间】:2016-08-10 16:00:06
【问题描述】:
我在 WPF MainWindow ctor 上有异常行为。我正在使用异步方法来完成一个长任务,但是这个名为“DoAsync”的任务不完整!我找到了一种解决方法,但我不明白为什么代码会失败:(这里是两个调用和结果:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
static void WriteToOutput(string message, int increment = 0)
{
var nw = DateTime.Now;
var msg = string.Format("{0}.{1:d3} - thread {2:d1}[0x{3:x4}] |\t{4}{5}",
nw.ToString("hh:mm:ss"),
nw.Millisecond,
System.Threading.Thread.CurrentThread.ManagedThreadId,
AppDomain.GetCurrentThreadId(),
new string(' ', 4 * increment),
message
);
System.Diagnostics.Debug.WriteLine(msg);
}
public async Task DoAsync()
{
WriteToOutput("DoAsync: begin", 2);
await Task.Delay(1); // enforces asynchronism
WriteToOutput("DoAsync: job begin", 3);
System.Threading.Thread.Sleep(5000); // simulates some job ;)
WriteToOutput("DoAsync: job end", 3);
WriteToOutput("DoAsync: end", 2);
}
private void NormalBehavior()
{
WriteToOutput("NormalBehavior: begin", 0);
Task.Run(async () =>
{
WriteToOutput("NormalBehavior_DoAsync: begin", 1);
await DoAsync();
WriteToOutput("NormalBehavior_DoAsync: end", 1);
});
WriteToOutput("NormalBehavior: sleeping", 0);
System.Threading.Thread.Sleep(10000); // to see what happens
WriteToOutput("NormalBehavior: terminated", 0);
}
/// <summary>
/// Seems the simplest solution, but it fails :( Why ?
/// </summary>
private void AbnormalBehavior()
{
WriteToOutput("AbnormalBehavior: begin", 0);
WriteToOutput("AbnormalBehavior_DoAsync: begin", 1);
var tsk = DoAsync();
WriteToOutput("AbnormalBehavior_DoAsync: end", 1);
WriteToOutput("AbnormalBehavior: sleeping", 0);
System.Threading.Thread.Sleep(10000); // to see what happens
WriteToOutput("AbnormalBehavior: terminated", 0);
}
public MainWindow()
{
NormalBehavior();
// Output:
// 05:18:00.833 - thread 8[0x3818] | NormalBehavior: begin
// 05:18:00.842 - thread 8[0x3818] | NormalBehavior: sleeping
// 05:18:00.846 - thread 9[0x2274] | NormalBehavior_DoAsync: begin
// 05:18:00.848 - thread 9[0x2274] | DoAsync: begin
// 05:18:00.853 - thread 10[0x0778] | DoAsync: job begin
// 05:18:05.855 - thread 10[0x0778] | DoAsync: job end
// 05:18:05.856 - thread 10[0x0778] | DoAsync: end
// 05:18:05.856 - thread 10[0x0778] | NormalBehavior_DoAsync: end
// 05:18:10.843 - thread 8[0x3818] | NormalBehavior: terminated
//_________________________________________________________________
AbnormalBehavior();
// Output:
// 05:18:10.845 - thread 8[0x3818] | AbnormalBehavior: begin
// 05:18:10.846 - thread 8[0x3818] | AbnormalBehavior_DoAsync: begin
// 05:18:10.847 - thread 8[0x3818] | DoAsync: begin
// 05:18:10.849 - thread 8[0x3818] | AbnormalBehavior_DoAsync: end
// 05:18:10.850 - thread 8[0x3818] | AbnormalBehavior: sleeping
// 05:18:20.853 - thread 8[0x3818] | AbnormalBehavior: terminated
//_________________________________________________________________
InitializeComponent();
}
}
有人已经发现了这个问题或有解释吗?
【问题讨论】:
-
因为您不要求结果。制作
var tsk = DoAsync(); tsk.Result。
标签: .net asynchronous synchronization async-await