4

I have one async task running an infinite loop doing some realtime processing:

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

And I created another async method SendWriteRqst that post command to RealTimeProcessingAsync(), should wait for status to set to defined condition and return.

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

It will works if SomeCondition() runs another while loop to polling status and return true if conditions set, but I'm looking for better solution.

(Sorry for bad English)

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
Amordad
  • 43
  • 1
  • 4
  • What is `SomeCondition()` doing, polling status from where/what? – Andrés Robinet Dec 17 '16 at 10:44
  • Actually it's a task that polls conditions of some variables from RealTimeProcessingAsync() like status variable. – Amordad Dec 17 '16 at 10:53
  • So `SomeCondition()` will call `RealTimeProcessingAsync()` but will only return if some conditions are met which depend on status changes made by `RealTimeProcessingAsync()`. That's why you have to call `RealTimeProcessingAsync()` multiple times. Is that it? – Andrés Robinet Dec 17 '16 at 10:59
  • No ,RealTimeProcessingAsync() start when connection established to HardWare Board,I want to implement SendWriteRqstAsync asynchronous based on condition in RealTimeProcessingAsync() thats why I implement SomeCondition() – Amordad Dec 17 '16 at 11:06
  • So... `RealTimeProcessingAsync()` is started at some point and runs forever? Then you want `SendWriteRqstAsync` to only return if some conditions are met within `RealTimeProcessingAsync()`. Did I get that right? Otherwise, can you post a bit of what `SomeCondition()` is doing? – Andrés Robinet Dec 17 '16 at 11:08
  • I think I have an idea (and you won't need another loop), but I want to fully understand what is going on before posting an answer that helps you – Andrés Robinet Dec 17 '16 at 11:10
  • Yes,RealTimeProcessingAsync() starts by user ,user can send request to HW by SendWriteRqstAsync that post command to RealTimeProcessingAsync() (using Queue) and HW return status and data to RealTimeProcessingAsync() again, and I want to implement SendWriteRqstAsync that return based on condition from status and data HW gave back. – Amordad Dec 17 '16 at 11:21

1 Answers1

4

It will works if SomeCondition() runs another while loop to polling status and return true if conditions set , but I'm looking for better solution.

What you want is a signal instead of a polling loop. If you only need to set the signal once, you can use TaskCompletionSource<T>. If you want a signal that can be set and reset (without having to be reconstructed), then you can use AsyncManualResetEvent (also available from NuGet in my AsyncEx.Coordination library).

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • "AsyncManualResetEvent" is what I need,Thank you for your answer and library it's very useful for me. – Amordad Dec 17 '16 at 16:19