How to implement a step button in C #?

I implemented the algorithm in C # and I want to make a gui for it, in my gui I want to put a button that with any click of gui shows a step forward in the algorithm, so I think I need to add something to Pause? in my code, which can resume with any click of the mouse. how should i do this? or is there any other suggestion for implementing this idea?

+3
source share
5 answers

It seems that you really need to turn your algorithm into a state machine - instead of actively “pausing” it, you would actively “promoting” it.

... , , yield return , .

, . GetEnumerator , MoveNext() , ( Current ). , .

+15
  • , .
  • , . AutoResetEvent.
  • "", , myWaitHandle.WaitOne() ( ).
  • myWaitHandle.Set() .

, , .

+2

, "" . :

interface ISteppedAlgorithm
{
    bool NextStep(); //returns if the algorithm is finished
    IStepResult LastStepResult {get;}
}

, . NextStep(). false, ( , ). LastStepResults display.h

0

, "", / .

http://www.differentpla.net/content/2005/02/implementing-wizard-c

, , .

  • .

    , .

  • , , , var:

    LongRunningMethod1();
    while(continue1 == true)
    {
        Thread.Sleep(50);
    }
    LongRunningMethod2()
    while(continue2 == true)
    {
        Thread.Sleep(50);
    }
    

continue1 2 true , .

0

If you simply “observe” the state of the algorithm as it develops, why not add some events (perhaps only one at the end) and allow the event handler to store an array of states. The user interface can simply skip forward / backward when and when necessary.

0
source

Source: https://habr.com/ru/post/1751590/


All Articles