本文介绍了如何从不同的线程是等待/异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自己熟悉C#的新的await /异步关键字,我发现几个方面,我不是很了解。

  1. 让我们开始比赛条件:

     流s = ...
    ...
    的for(int i = 0; I< 100;我++)
    {
        s.WriteAsync(新的byte [] {I},0,1);
    }
     

    将这项工作作为预期所有的时间(如写入文件12345 .....而不是13254或东西)?

    第二件事是,异步函数执行同步,如果它不包含等待运营商。而且,根据微软的文档,异步函数总是在调用者的线程(相对于的BeginInvoke)执行。这使我3下一个问题:

  2. 它释放到调用函数之前,有多少异步函数的执行?

     异步无效MyAsyncFunction()
    {
        Operation1();
        操作2();
        Operation3();
        ....
        流s = ...;
        等待s.WriteAsync(....);
    }
     

    在有关文章等候/异步,我读过,它说,如果没有异步函数等待操作员按顺序执行,并与异步/计谋返回迫在眉睫。但它困扰着我, MyAsyncFunction 释放,因为它击中之前可能一直执行Operation1 ... Operation3 等待s.WriteAsync

  3. 如果我用 Thread.sleep代码的异步功能是这样的:

     异步无效DoStuff()
    {
        流s = ...;
        ...
        等待s.WriteAsync(....);
        Thread.sleep代码(10000);
        ....
    }
     

    将Thread.sleep代码块中被执行的整个线程或只是异步功能?

  4. 如果我用什么 semaphore.Wait()中的异步功能之一,它会期待的信号,以通过其他异步功能被释放。请问这种行为,因为它会与线程,还是会导致死锁?

  5. 伺机没有的异步功能在外面工作。为什么呢?

解决方案

我建议你阅读我的 异步介绍

没有。您需要等待的调用 WriteAsync

直到等待 s表示尚未完成的操作。

Thread.sleep代码 - 和所有其他拦截方法 - 将阻止异步方法和正在执行的线程吧。

作为一般规则,不使用的任意的阻断方法在异步方法。

这完全取决于你的环境。 等待是一个阻塞方法,所以如果其他异步方法需要通过阻断法举行的背景下,再你会死锁。

注意 SemaphoreSlim 异步 - 友好;您可以使用 WaitAsync 而不是等待

由于该异步关键字,可以在计谋关键字。这样做是为了尽量减少新的关键字在C#语言和code可读性的影响。

I'm trying to familiarize myself with c#'s new await/async keywords, and I've found several aspects which I can't quite understand.

  1. Let's start with race conditions:

    Stream s=...
    ...
    for(int i=0;i<100;i++)
    {
        s.WriteAsync(new byte[]{i},0,1);
    }
    

    will this work as expected all the time (e.g. write to the file 12345..... and not 13254 or something)?

    The second thing is that async function executes synchronously if it does not contain await operator. And, according to microsoft documentation, async functions always execute in the caller thread (as compared to BeginInvoke). This brings me to 3 next questions:

  2. How much of an async function is executed before it releases to the caller function?

    async void MyAsyncFunction()
    {
        Operation1();
        Operation2();
        Operation3();
        ....
        Stream s=...;
        await s.WriteAsync(....);
    }
    

    In the articles about await/async that I've read, it's said that async functions without await operator execute sequentially, and with async/await return imminently. But it's nagging at me that MyAsyncFunction may always execute Operation1...Operation3 before releasing as it hits await s.WriteAsync.

  3. What if I use Thread.Sleep in the async function like this:

    async void DoStuff()
    {
        Stream s=...;
        ...
        await s.WriteAsync(....);
        Thread.Sleep(10000);
        ....
    }
    

    Will Thread.Sleep block the whole thread in which it is executed or just the async function?

  4. What if I use semaphore.Wait() in one of the async functions and it will expect for the semaphore to be released by an other async function. Will this behave as it would with threads, or will it cause deadlock?

  5. await does not work outside of async functions. Why?

解决方案

I recommend you read my async intro.

No. You need to await the calls to WriteAsync.

Until it awaits an operation that is not already completed.

Thread.Sleep - and all other blocking methods - will block the async method and the thread that is executing it.

As a general rule, do not use any blocking methods within an async method.

It totally depends on your contexts. Wait is a blocking method, so if the "other" async method requires the context held by the blocked method, then you will deadlock.

Note that SemaphoreSlim is async-friendly; you can use WaitAsync instead of Wait.

Because the async keyword enables the await keyword. This was done to minimize the impact of new keywords on the C# language and for code readability.

这篇关于如何从不同的线程是等待/异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:35