c# - Task unable to timeout -


i have implemented simple task using tpl. waits 10 seconds execute , returns true/false.

var checkcfoptask = task.run(() => checkcfopexists()); checkcfoptask.wait(timespan.fromseconds(10)); if (checkcfoptask.result) {  } else {  } 

the problem code stuck within if statement.

if (checkcfoptask.result) 

each time pause debugger still keeps waiting on above line of code. happened first time. ideally should return true/false within 10 seconds.

below function definitions-

checkcfoexists: executed task.

private bool checkcfopexists() {     bool found = false;      try     {         while (!found)         {             try             {                 if (iedriver.findelement(by.id("popup_message")).text == "não existem itens para realizar o rateio.")                 {                     resetinvoicesearchscreen();                     break;                 }             }             catch (exception ex)             {              }              try             {                 if (arrcfoplist.contains(iedriver.findelement(by.id("vendornf.cfopopercode")).getattribute("value")))                 {                     found = true;                 }             }             catch (exception ex)             {              }         }     }     catch (exception ex)     {      }     return found; } 

resetinvoicesearchscreen: executed within above function

private void resetinvoicesearchscreen() {     try     {         iedriver.findelement(by.id("popup_ok")).click();         iedriver.findelement(by.id("ltmcnpjcpf")).clear();         iedriver.findelement(by.id("notafiscalnbr")).clear();         iedriver.findelement(by.id("inbnotafiscalid")).clear();         iedriver.findelement(by.id("seriesfrmcd")).clear();     }     catch (exception ex)     {      } } 

is there else needed ensure function times out correctly? please let me know if can provide more details.

edit

i see below message checkcfoptask.result in immediate window of visual studio-

id = cannot evaluate expression because code of current method optimized., status = cannot evaluate expression because code of current method optimized., method = cannot evaluate expression because code of current method optimized., result = cannot evaluate expression because code of current method optimized. 

it looks need add time-out support method you're calling - because if doesn't find thing it's looking loop forever.

the easiest way pass cancellationtoken method. should factor out testing code separate method returns bool.

also note have busy loop, not idea when polling! it's better introduce small sleep when polling if thing you're polling isn't available. (note: polling in general not approach if have better way of checking something, doesn't there's else can use here, polling have do.)

you can write method (i've omitted code polls thing you're looking in order concentrate on other logic):

private bool checkcfopexists(cancellationtoken cancel) {     timespan retrydelay = timespan.frommilliseconds(500);      while (true)     {         if (trytofindthething()) // blocking call.             return true;          if (cancel.waithandle.waitone(retrydelay))             return false;     } }  bool trytofindthething() {     return false;  // implementation goes here. } 

then call , have 10-second timeout (compilable console app):

using system; using system.diagnostics; using system.threading; using system.threading.tasks;  namespace consoleapp3 {     class program     {         static void main()         {             var test = new program();              // create cancellation token source cancels after 10 seconds:             var cancellation = new cancellationtokensource(timespan.fromseconds(10));              // create , run task:              var sw = stopwatch.startnew();             var checkcfoptask = task.run(() => test.checkcfopexists(cancellation.token));              console.writeline("waiting task finish.");             console.writeline($"task returned: {checkcfoptask.result} after {sw.elapsedmilliseconds} ms");         }          private bool checkcfopexists(cancellationtoken cancel)         {             timespan retrydelay = timespan.frommilliseconds(500);              while (true)             {                 if (trytofindthething()) // blocking call.                     return true;                  if (cancel.waithandle.waitone(retrydelay))                     return false;             }         }          bool trytofindthething()         {             return false;  // implementation goes here.         }     } } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -