为什么程序在异步完成任务之前退出?
Why program exits before async finishes its task?
我认为这是一个非常多余的问题,但我确实深入挖掘并没有找到答案。我使用的是第一个 here 中提到的非常虚拟的示例。
我创建了一个 windows 表单应用程序,我在其中删除了主程序中的自动生成代码,并复制并粘贴了示例站点中的代码。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
//[STAThread]
static void Main()
{
// Create task and start it.
// ... Wait for it to complete.
Task task = new Task(ProcessDataAsync);
task.Start();
task.Wait();
}
static async void ProcessDataAsync()
{
// Start the HandleFile method.
Task<int> task = HandleFileAsync("D:\test.txt");
// Control returns here before HandleFileAsync returns.
// ... Prompt the user.
Console.WriteLine("Please wait patiently " +
"while I do something important.");
// Wait for the HandleFile task to complete.
// ... Display its results.
int x = await task;
Console.WriteLine("Count: " + x);
}
static async Task<int> HandleFileAsync(string file)
{
Console.WriteLine("HandleFile enter");
int count = 0;
// Read in the specified file.
// ... Use async StreamReader method.
using (StreamReader reader = new StreamReader(file))
{
string v = await reader.ReadToEndAsync();
// ... Process the file data somehow.
count += v.Length;
// ... A slow-running computation.
// Dummy code.
for (int i = 0; i < 10000; i++)
{
int x = v.GetHashCode();
if (x == 0)
{
count--;
}
}
}
Console.WriteLine("HandleFile exit");
return count;
}
}
输出为:
HandleFile enter
The thread 0x8184 has exited with code 259 (0x103).
The thread 0x6764 has exited with code 259 (0x103).
The program '[42112] testasyncforms.vshost.exe' has exited with code 0 (0x0).
Please wait patiently while I do something important.
那么,为什么程序不打印就退出了"HandleFile exit"?
因为这个new Task(ProcessDataAsync);
不符合你的要求。
由于 async void
模式仅用于异步事件处理,在您的情况下,我建议您使用 ProcessDataAsync
方法 return a Task
然后只需 .Wait()
完成该任务即可。
我认为这是一个非常多余的问题,但我确实深入挖掘并没有找到答案。我使用的是第一个 here 中提到的非常虚拟的示例。
我创建了一个 windows 表单应用程序,我在其中删除了主程序中的自动生成代码,并复制并粘贴了示例站点中的代码。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
//[STAThread]
static void Main()
{
// Create task and start it.
// ... Wait for it to complete.
Task task = new Task(ProcessDataAsync);
task.Start();
task.Wait();
}
static async void ProcessDataAsync()
{
// Start the HandleFile method.
Task<int> task = HandleFileAsync("D:\test.txt");
// Control returns here before HandleFileAsync returns.
// ... Prompt the user.
Console.WriteLine("Please wait patiently " +
"while I do something important.");
// Wait for the HandleFile task to complete.
// ... Display its results.
int x = await task;
Console.WriteLine("Count: " + x);
}
static async Task<int> HandleFileAsync(string file)
{
Console.WriteLine("HandleFile enter");
int count = 0;
// Read in the specified file.
// ... Use async StreamReader method.
using (StreamReader reader = new StreamReader(file))
{
string v = await reader.ReadToEndAsync();
// ... Process the file data somehow.
count += v.Length;
// ... A slow-running computation.
// Dummy code.
for (int i = 0; i < 10000; i++)
{
int x = v.GetHashCode();
if (x == 0)
{
count--;
}
}
}
Console.WriteLine("HandleFile exit");
return count;
}
}
输出为:
HandleFile enter The thread 0x8184 has exited with code 259 (0x103). The thread 0x6764 has exited with code 259 (0x103). The program '[42112] testasyncforms.vshost.exe' has exited with code 0 (0x0). Please wait patiently while I do something important.
那么,为什么程序不打印就退出了"HandleFile exit"?
因为这个new Task(ProcessDataAsync);
不符合你的要求。
由于 async void
模式仅用于异步事件处理,在您的情况下,我建议您使用 ProcessDataAsync
方法 return a Task
然后只需 .Wait()
完成该任务即可。