Click here to Skip to main content
15,895,142 members

Using async/await, Seven questions about calling mechanisms.

Chris875 asked:

Open original thread
Please refer below for the several options to call a function with async/await.
I put my questions below each option.
Lets assume I have a 3rd party Dll and want to call a function of it asynchronously. Despite there is a function description one cannot know what actually happens inside:
C#
/// <summary>
///  The function of a C# DLL to call. It reads data from a device.
/// </summary>
/// <param name="dataMsgs">
/// output structure for "signal data" (double) per "channel id" (Int32)
/// </param>
void DllTestFunction(Dictionary<Int32, double> dataMsgs);


First attempt to call that function asynchronously:
C#
public class DriverTestViewModel
{
  private readonly Dictionary<Int32, double> _dataDictionaryForDll;

  private async Task CallDllTestFunctionAsync()
  {
    // Option 1, one-liner async call 
    await Task.Run(() => DllTestFunction(_dataDictionaryForDll)).ConfigureAwait( 
    false );
  }
}

Q1: Is there anything wrong yet in the attempt to encapsulate the "ordinary" function in an async call? I'm asking that since I found trivial examples only yet. E.g.:
C#
await Task.Delay(TimeSpan.FromSeconds(2)); 
// or by calling an existing async .Net function like FileStream.ReadAsync()
await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);

Both trivial examples are not calling Task.Run() or Task.Start(), which is pretty confusing.
Q2: I assume this is done inside Task.Delay() and FileStream.ReadAsync(), is it?

But there is more to ask:
C#
// Option 2, split into two calls
var dllTask = Task.Run(() => DllTestFunction(_dataDictionaryForDll));
await dllTask.ConfigureAwait( false );

Q3: I assume both implementation options 1 and 2 are fully equivalent, are they?

Then one can split up more:
C#
// Option 3, separate calls even more
var dllTask = Task.Run(() => DllTestFunction(_dataDictionaryForDll));
dllTask.ConfigureAwait(false); // compiler warning
await dllTask;

A compiler warning is issued stating that the call is not awaited.
Q4: Why is that and can the warning be safely ignored as long as the await call follows?

Finally the option 3, the task is created on its own and Start() is used on the instance rather than the shared Task.Run() call.
C#
// Option 3, fully split
var dllTask = new Task(() => DllTestFunction(_dataDictionaryForDll));

dllTask.Start(); // instance call vs. shared Task.Run()
await dllTask.ConfigureAwait( false ); 

Q5: I assume implementation options 1,2 and 3 are equivalent regarding the async processing, are they?

Q6: What is the difference in executing functions A,B,C in a row as illustrated in the example below?
Q7: When should the shared call be avoided?
C#
// instance call
  A.Start();
  B.Start();
  C.Start();
// vs. shared call
  Task.Run(A);
  Task.Run(B);
  Task.Run(C);
Tags: C# 5.0, .NET (.NET4.5), Async, Concurrency, Await, TAP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900