简体   繁体   English

如何安全地确保 C# 中的异步方法完成?

[英]How to safely make sure an async method in C# is completed?

please excuse the possibly stupid question.请原谅这个可能愚蠢的问题。 Is there a difference between the two code snippets?两个代码片段之间有区别吗?

Example 1: Running an async method CapturePhotoToStorageFileAsync in a synchronous method示例 1:在同步方法中运行异步方法 CapturePhotoToStorageFileAsync

class Core
{
    async void ProcessCommand(Command cmd)
    {
        // Do some stuff
        await JobManager.ReportTrigger(cmd.TriggerID);
        SendCommand(Command reply);
    }
}

class JobManager
{
    async Task ReportTrigger(Int32 triggerID)
    {
        await Task.Delay(300);
        Webcam.CapturePhotoToFile();
        await Task.Delay(100);
    }
}

class WebCam
{
    void CapturePhotoFile()
    {
        m_MediaCapture.CapturePhotoToStorageFileAsync( ImageEncodingProperties.CreateJpeg(), file );
    }
}

Example 2: Running the async method CapturePhotoToStorageFileAsync as a task:示例 2:将异步方法 CapturePhotoToStorageFileAsync 作为任务运行:

class Core
{
    async void ProcessCommand(Command cmd)
    {
        // Do some stuff
        await JobManager.ReportTrigger(cmd.TriggerID);
        SendCommand(Command reply);
    }
}

class JobManager
{
    async Task ReportTrigger(Int32 triggerID)
    {
        await Task.Delay(300);
        await Webcam.CapturePhotoToFile();
        await Task.Delay(100);
    }
}

class WebCam
{
    async Task CapturePhotoFile()
    {
        await m_MediaCapture.CapturePhotoToStorageFileAsync( ImageEncodingProperties.CreateJpeg(), file );
    }
}

I really need to make sure that the Call "SendCommand(Command reply)" is being executed AFTER the webcam picture has been saved.我真的需要确保在保存网络摄像头图片后正在执行调用“SendCommand(命令回复)”。 Which method is more suitable for this purpose?哪种方法更适合此目的?

Thanks in advance and best regards!在此先感谢并致以最诚挚的问候!

EDIT:编辑:

According to the comments received i am currently thinking about this implementation:根据收到的评论,我目前正在考虑这个实现:

class Core
{
    async Task ProcessCommandAsync(Command cmd)
    {
        // Do some stuff
        await JobManager.ReportTriggerAsync(cmd.TriggerID);
        SendCommand(Command reply);
    }
}

class JobManager
{
    async Task ReportTriggerAsync(Int32 triggerID)
    {
        await Task.Delay(300);
        await Webcam.CapturePhotoToFileAsync();
        await Task.Delay(100);
    }
}

class WebCam
{
    async Task CapturePhotoFileAsync()
    {
        await m_MediaCapture.CapturePhotoToStorageFileAsync( ImageEncodingProperties.CreateJpeg(), file );
    }
}

I would really appreciate if somebody could ensure me the intended behaviour for this implementation.如果有人能确保我实现此实现的预期行为,我将不胜感激。 Is it really assured that the command in Core.ProcessCommandAsync is send AFTER the picture has been saved?是否真的确定 Core.ProcessCommandAsync 中的命令是在图片保存后发送的?

@Dai: Thanks for your feedback. @Dai:感谢您的反馈。 I merged my answer into my first posting.我将我的答案合并到我的第一个帖子中。 Sorry for the trouble.抱歉,添麻烦了。

There are a few rules that make async easier to get right.有一些规则可以使异步更容易正确。

  1. Async functions return Task or Task<> (don't use void)异步函数返回 Task 或 Task<>(不要使用 void)
  2. Async functions are named with Async on the end (MyFuncAsync) - this reminds you to call await on those functions, see rule 3.异步函数以 Async 结尾 (MyFuncAsync) - 这提醒您在这些函数上调用 await,请参阅规则 3。
  3. Await any async functions (unless you want them to run in parallel)等待任何异步函数(除非您希望它们并行运行)
  4. Don't break these rules.不要破坏这些规则。

Keeping to these rules can be hard, if you have already broken them, and have to refactor your program.如果您已经违反了这些规则并且必须重构您的程序,那么遵守这些规则可能会很困难。

(there are lots of exceptions to these rules, but if you don't know what you are doing, then try to keep to the rules). (这些规则有很多例外,但是如果您不知道自己在做什么,请尽量遵守规则)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM