C # async without waiting

In the documentation here: http://msdn.microsoft.com/en-us/library/hh191443.aspx this means that:

If the async method does not use the wait statement to mark the suspension of a dot, the method runs as a synchronous method, despite the asynchronous modifier. The compiler generates a warning for such methods.

I believe this warning:

In this asynchronous method, there are no “wait” statements and will be executed synchronously. Think of it using the “wait” operator to wait for non-blocking API calls, or “wait for Task.Run (...)” to do the work of binding to the processor in the background thread.

Then, in another link, http://msdn.microsoft.com/en-us/library/windows/apps/hh994635.aspx , the example that it shows looks like this:

public class Example
{
    // ...
    private async void NextMove_Click(object sender, RoutedEventArgs e)
    {
        await Task.Run(() => ComputeNextMove());
        // Update the UI with results
    }

    private async Task ComputeNextMove()
    {
        // ...
    }
    // ...
}

Here, I assume that ComputeNextMove- this is basically a synchronous method, by itself, not causing a wait. This would seem to contradict the warning about the compiler (if this is not a bad example ...)

If I don't call the .net Async method on the END of my asynchronous call stack, for example HttpClient.GetStringAsync, and I want to implement some specific “long-running” synchronous logic, is there a better way to do this?

Perhaps my assumption is incorrect, but ComputeNextMovemay be declared as private void ComputeNextMove(), which will not give any warnings.

+4
1

, .

ComputeNextMove , ( ), async. private void ComputeNextMove().

ComputeNextMove - Task.Run.

:

await Task.Run((Action) ComputeNextMove);
+12

Source: https://habr.com/ru/post/1526387/


All Articles