WPF MainWindow ctor에서 비정상적인 동작이 있습니다. 비동기 메서드를 사용하여 긴 작업을 만들고 있지만 'DoAsync'라는이 작업이 완료되지 않았습니다! 나는 해결 방법을 발견하지만 난 코드가 두 번 호출하고 결과가 여기 :(실패 이유를 이해하지 않습니다?.net 비동기 비정상 동작
/// <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'. – cassandrad