none
C# – Best way to wait until a condition is true RRS feed

  • Question

  • Hello,

    What is the best way to wait until a condition is True from inside a Task (possibly with a timeout)?
    My code is running inside a Task and the flag that I must wait is from a library.
    SipnWait can be a good choice?
    Thank you

    Friday, February 17, 2017 10:17 PM

All replies

  • Hi ivan692007,

    AFAIK, I would suggest you use thread waiting handler

    private readonly System.Threading.EventWaitHandle waitHandle = new System.Threading.AutoResetEvent(false);
    private void btnOk_Click(object sender, EventArgs e)
    {
        // Do some work
        Task<string> task = Task.Run(() => GreatBigMethod());
        string GreatBigMethod = await task;
    
        // Wait until condition is false
        waitHandle.WaitOne();
        Console.WriteLine("Excel is busy");
        waitHandle.Reset();
    
        // Do work
        Console.WriteLine("Your work");
     }
    void isExcelInteractive()
    {
       /// Do your check
       waitHandle.Set()
    }

    Just know that only the first Set () signal, WaitOne () will wait until the signal will continue after the implementation.

    Best regards,

    Kristin

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Kristin Xie Monday, February 20, 2017 2:43 AM
    Monday, February 20, 2017 2:17 AM
  • Hi Kristin,

    I usually use what you suggested but in this case I can't because the boolean flag is from a spaghetti code library that in this moment I can't modify.

    Thank you

    Best regards,

    Ivan

    Monday, February 20, 2017 9:33 PM
  • How about the Boolean variable you use to store the flag? If it can be modified, changing it to property and signal your code to run in the setter will work.
    Tuesday, February 21, 2017 1:33 AM
    Answerer
  • I can't modify the boolean variable. It comes from a dll that for me is a black box.

    Thank you

    Ivan

    Tuesday, February 21, 2017 9:20 PM
  • Does the classes of the library come with properties named .SyncRoot that you can hold lock() against? If so using lock(theclass.SyncRoot) { ... } will do. (Note: Since lock statement will only work on reference types, putting lock on the Boolean variable directly won't work and you need not waste time to try)

    If not, I'd think monitoring that variable with background worker thread that is put into sleep and wake with Thread Timer is your best bet.


    • Edited by cheong00Editor Wednesday, February 22, 2017 1:18 AM
    • Proposed as answer by Kristin Xie Friday, February 24, 2017 1:09 AM
    Wednesday, February 22, 2017 1:17 AM
    Answerer
  • I will use the second option

    Thank you

    Thursday, February 23, 2017 9:39 PM