英文:
How to get exception or status after async method is done running in the background?
问题
以下是您的代码,我正在尝试模拟。我可以移除async-await来进行"发射和忘记",但我试图理解异步(非阻塞)代码的目的,但似乎在运行NextProcess()
方法之前,AWAIT是一个阻塞代码。如果我移除await,就能实现我的目标,但在后台运行完成后是否有任何方式获取异常或状态呢?
public static async Task Main()
{
Console.WriteLine("开始长时间运行的过程...");
await Task.Run(() => LongRunningProcess());
NextProcess();
}
private static void LongRunningProcess()
{
// 模拟正在进行的工作
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
private static void NextProcess()
{
Console.WriteLine("在See方法中,继续而不等待!");
}
我尝试了我粘贴的代码的各种排列方式。
英文:
Here's my code I'm trying to simulate. I can remove the async-await to do "fire and forget", but I'm trying to understand the purpose of asynchronous (NON-BLOCKING) code, but it looks like AWAIT is a blocking code before running the method NextProcess()
. If I remove await, it meets my objective, but is there any way to get any sort of exception or status after it's done running in the background?
public static async Task Main()
{
Console.WriteLine("Starting the long-running process...");
await Task.Run(() => LongRunningProcess());
NextProcess();
}
private static void LongRunningProcess()
{
// Simulating the work being done
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
private static void NextProcess()
{
Console.WriteLine("In See method, continuation without waiting!");
}
I tried all sorts of permutations of the code I pasted.
答案1
得分: 5
-
"it looks like AWAIT is a blocking code before running the method NextProcess()."
- 似乎AWAIT是在运行方法NextProcess()之前的阻塞代码。
-
"You can think of "await" as "asynchronous wait". It does pause the method (i.e., it waits), but doesn't block the thread (i.e., it's asynchronous, not synchronous). You may find my
async
intro helpful."- 您可以将"await"视为"异步等待"。它会暂停方法(即,它会等待),但不会阻塞线程(即,它是异步的,而不是同步的)。您可能会发现我的
async
简介有帮助。
- 您可以将"await"视为"异步等待"。它会暂停方法(即,它会等待),但不会阻塞线程(即,它是异步的,而不是同步的)。您可能会发现我的
-
"If I remove await, it meets my objective, but is there any way to get any sort of exception or status after it's done running in the background?"
- 如果我去掉await,就能达到我的目标,但在后台运行完成后是否有任何方式可以获得任何异常或状态?
-
"Sure; the
Task
returned from anasync
method represents the execution of that method. When the method completes, theTask
is completed, and if the method throws an exception, theTask
is completed with that exception. In this case, you're usingTask.Run
to execute synchronous code on a thread pool thread, but the same idea applies: the returnedTask
represents the execution of the delegate passed toTask.Run
."- 当然可以;从异步方法返回的
Task
表示该方法的执行。当方法完成时,Task
也完成了,如果方法引发异常,Task
会以该异常完成。在这种情况下,您正在使用Task.Run
在线程池线程上执行同步代码,但相同的思想也适用:返回的Task
表示传递给Task.Run
的委托的执行。
- 当然可以;从异步方法返回的
-
"There's nothing magical about a
Task
instance. You can store theTask
instance in a variable or whatever and observe it later:"Task
实例没有什么神奇之处。您可以将Task
实例存储在变量或其他位置,并稍后观察它:
public static async Task Main()
{
Console.WriteLine("Starting the long-running process...");
var task = Task.Run(() => LongRunningProcess());
NextProcess();
await task;
}
英文:
> it looks like AWAIT is a blocking code before running the method NextProcess().
You can think of "await" as "asynchronous wait". It does pause the method (i.e., it waits), but doesn't block the thread (i.e., it's asynchronous, not synchronous). You may find my async
intro helpful.
> If I remove await, it meets my objective, but is there any way to get any sort of exception or status after it's done running in the background?
Sure; the Task
returned from an async
method represents the execution of that method. When the method completes, the Task
is completed, and if the method throws an exception, the Task
is completed with that exception. In this case, you're using Task.Run
to execute synchronous code on a thread pool thread, but the same idea applies: the returned Task
represents the execution of the delegate passed to Task.Run
.
There's nothing magical about a Task
instance. You can store the Task
instance in a variable or whatever and observe it later:
public static async Task Main()
{
Console.WriteLine("Starting the long-running process...");
var task = Task.Run(() => LongRunningProcess());
NextProcess();
await task;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论