TheException TheException - 18 days ago 5
C# Question

Use of Async / Await to write to SQL using three Tasks

I have a program that reads from 7 different PLC's at generally the same time using background threads. Using the 7 different reads, I am hoping to write to three different SQL tables.

public static async void readReady()
{
if (Read1_complete && Read2_complete && BCS_complete == true)
{
await Task.Run(() => W1_SQL_Write.SQL_Write_Enabled());
}
if (Read3_complete && Read4_complete && BCS_complete == true)
{
await Task.Run(() => W2_SQL_Write.SQL_Write_Enabled());
}
if (Read5_complete && Read6_complete && BCS_complete == true)
{
await Task.Run(() => W3_SQL_Write.SQL_Write_Enabled());
}
}


The 7 read methods call this
readReady()
method, and if the other reads flags are set, one of the three SQL write tasks gets started.

I am new to async and await tasks. I am wondering if this is a good way to go about calling different methods to write to SQL? Will the second and third "if" statements wait for the first to return, when the first "if" statement is called true before hand?

It is possible that all three write methods come true at the same time, so will this cause some bugs too, or is async and await the way to go about doing it?

Answer

It is possible that all three write methods come true at the same time, so will this cause some bugs too, or is async and await the way to go about doing it?

When you read async and await code, try to think of it like this... for every await statement the code that follows will not execute until the "awaited" operation completes and returns, or errors out. This is true for both Task and Task<T> (as well as other awaitables, i.e.; IAsyncOperation<T>, IAsyncAction, etc).

I am wondering if this is a good way to go about calling different methods to write to SQL?

I'm not sure I fully understand this questions specifically. Nine times out of ten, I prefer using the asynchronous programming paradigm for I/O bound operations -- database interaction is most certainly I/O bound, so yes.

Will the second and third "if" statements wait for the first to return, when the first "if" statement is called true before hand?

The if statements themselves are not waiting for anything. They will execute as quickly as possible, but because of the await keyword they will not be executed until the async operation is finished. So one can read this code and expect a "serialized" execution where the order of the code reads just like synchronous code. Does that make sense?

I would probably rewrite this as follows:

public static async Task ReadReadyAsync()
{
    if (!BCS_complete) return;

    await Task.Run(() => W1_SQL_Write.SQL_Write_Enabled());
    await Task.Run(() => W2_SQL_Write.SQL_Write_Enabled());
    await Task.Run(() => W3_SQL_Write.SQL_Write_Enabled());
}

You had a check before every call to see if the BCS_complete bit was set. So I added an early exit. Then since all of these calls will execute in a serialized manner we can write them that way.

Comments