loops - C# Wait until condition is true -
i trying write code executes when condition met. currently, using while...loop, know not efficient. looking @ autoresetevent() don't know how implement such keeps checking until condition true.
the code happens live inside async method, may kind of await work?
private async void btnok_click(object sender, eventargs e) { // work task<string> task = task.run(() => greatbigmethod()); string greatbigmethod = await task; // wait until condition false while (!isexcelinteractive()) { console.writeline("excel busy"); } // work console.writeline("yay"); } private bool isexcelinteractive() { try { globals.thisworkbook.application.interactive = globals.thisworkbook.application.interactive; return true; // excel free } catch { return false; // excel throw exception, meaning busy } }
i need find way keep checking isexcelinteractive()
without cpu stuck in loop.
note: there no event handler in excel raised when not in edit mode.
at least can change loop busy-wait slow poll. example:
while (!isexcelinteractive()) { console.writeline("excel busy"); await task.delay(25); }
Comments
Post a Comment