如何在异步方法在后台运行完成后获取异常或状态?

huangapple go评论74阅读模式
英文:

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(&quot;Starting the long-running process...&quot;);

    await Task.Run(() =&gt; LongRunningProcess());

    NextProcess();
}

private static void LongRunningProcess()
{
    // Simulating the work being done
    for (int i = 0; i &lt; 5; i++)
    {
        Console.WriteLine(i);
        Thread.Sleep(1000);
    }
}

private static void NextProcess()
{
    Console.WriteLine(&quot;In See method, continuation without waiting!&quot;);
}

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简介有帮助。
  • "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 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."

    • 当然可以;从异步方法返回的Task表示该方法的执行。当方法完成时,Task也完成了,如果方法引发异常,Task会以该异常完成。在这种情况下,您正在使用Task.Run在线程池线程上执行同步代码,但相同的思想也适用:返回的Task表示传递给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:"

    • 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(&quot;Starting the long-running process...&quot;);

  var task = Task.Run(() =&gt; LongRunningProcess());

  NextProcess();

  await task;
}

huangapple
  • 本文由 发表于 2023年6月15日 09:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定