c# - if else if in while loop were else if test is defined in the first cycle -
somewhat confusing, when trying describe in title. here's example code. i'm hoping possible (i haven't found way yet.)
datetime starttime = datetime.now; int = 0; bool running = true; while (running) { datetime? lastruntime = null; if (i == 0) { if ((datetime.now - starttime).minutes >= program.gentimer) { = 1; lastruntime = datetime.now; } } else if ((datetime.now - lastruntime).minutes >= program.gentimer) { //doestuff }
this looks foolish (and not work @ all, obvious being nullable timespan doesnt contain definition 'minutes. i've tried
datetime lastruntime;
instead of
datetime? lastruntime = null;
so i'm forcing first if statement true first time , false every pass afterward. i'm open different approach.
if you're curious, goal run in service runs through 11 operations 1 @ time on different time intervals. due how these operations processed, can't have more 1 run @ time.
if you're using nullable datetime, don't need i
@ all. .hasvalue
tell if it's been set or not. false first time. move out of while loop, otherwise you're redeclaring each time.
try like
datetime starttime = datetime.now; bool running = true; datetime? lastruntime = null; while (running) { if (!lastruntime.hasvalue) { if ((datetime.now - starttime).minutes >= program.gentimer) { lastruntime = datetime.now; } } else if ((datetime.now - lastruntime.value).minutes >= program.gentimer) { //doestuff } }
Comments
Post a Comment