C # synchronization between async tasks with infinite while?

I have one async task executing an infinite loop doing real-time processing:

private async void RealTimeProcessingAsync()
{
    while (true)
    {
        //....
       Tmstaus  status = await ReadStatusAsync();
       switch (status)
       {
            case Ack:
            //...
            break;
            //...
        }
        //...
    }
}

And I created another async method SendWriteRqst, which sends the command to RealTimeProcessingAsync (), must wait until the status is set to a specific condition and returns.

public async Task<WriteState> SendWriteRqstAsync(TmCommand tmCmd)
{
    //...
    await SomeCondition()//based on status value
    //...
    return wrState;
}

It will work if SomeCondition () starts another while loop in the polling state and returns true if the conditions are set, but I'm looking for a better solution.

(Sorry for the bad English)

+4
source share
1 answer

It will work if SomeCondition () starts another while loop in the polling state and returns true if the conditions are set, but I'm looking for a better solution.

, , . , TaskCompletionSource<T>. , , reset ( ), AsyncManualResetEvent ( NuGet AsyncEx.Coordination).

+4

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


All Articles