Algorithm for a requirement

  • Thread starter Thread starter La07K
  • Start date Start date
L

La07K

Guest
Hi i am executing a function in an infinite while loop for some particular timeout.

here my requirement is if my method return two times same output then i have to return from the loop.

Below is my code snippet,here i am using one list then checking the consecutive values are equal or nt(true).But is there any alternative better performing way is there or not?

I have one doubt

steps 4 is here

private StatusInfo WaitforState(State state, TimeSpan timeout)
{
bool? re = null;
while (true)
{
Thread.Sleep(10000);
var t = timeout.Subtract(new TimeSpan(0, 0, 10));
if (t == TimeSpan.Zero)
break;
var r = GetStatus();

if (null == r)
continue;
if (r.State == state)
{
if (state == State.Open && r.Mode == StateMode.Enter)
{
if (re==true)
return StatusInfo.OK;
else
re = true;
}
else
re = false;
}
}

return StatusInfo.Error;
}

Here my question is,

Actualy my requirement got changed,i have done it but the code looks complicated and not clean.So can u help me to find a optimal solution.

My requirement is

The same function is used for identification of 4 state of my application.

Step1.System main power is down(state==state.powerDown)>>Only one query if its in power down we cannot proceed so exit from the loop.otherwise go to step2

Step2.System is up but few part of the system is not in good condition(state==state.powerup&&stateMode.IsConfiguredProperly==false)>>

Only 2 query(2 times will querry) if its in power up and statemode.IsConfiguredProperly==false we cannot proceed so exit from the loop,otherwise goto step3

Step3.system is up and loading settings(state==State.powerUp&&stateMode.IsLoadingStatrted==true)>>This we have to query for 5 minutes if above two conditions are satified then whenever these condition is met we will execute 4th steps if it didnt met within 5 minutes we will exit the loop with failure message.otherwise go to step 4

Step4.system loaded and ready to use(state==State.Open&&state==stateMode.Enter)>>this as i earlier said this we have to verify 2 consecutive times our condition has to satisfy with 10secondx interval maximum 10 minutes



Its a hardware related application i am communicating to a hardware board.Once its get rebooted i have to confirm all these criteria are met .Then only i am able to use this hardware.



All information will get as part of one query to hardware GetStatus(Give all the information required for the above steps.State & statemode)

I have done it with 4 functions then code duplication readabily issues etc came.

So plz tell me about ur thoughts.
I have done with 4 functions how can i optimise this one plz help me for this


If question is not clear let me know.





Coding.....................................

Continue reading...
 
Back
Top