views:

105

answers:

5

I implemented an 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 the gui shows a step forward in algorithm, so i think i need to put something like pause? statements in my code that with any click it can resume. how should i do that? or is there any other suggestion for implementing this idea?

+14  A: 

It sounds like really you need to turn your algorithm into a state machine - instead of actively "pausing" it, you would actively "advance" it.

You may find iterator blocks useful... if your algorithm is pretty much in one method at the moment, you may be able to change it to insert a yield return statement at the end of each logical step, returning some indication of the current status.

That's not an entirely normal use of iterator blocks, but it could be the simplest way forward here. Your UI would call GetEnumerator once at the start, and then MoveNext() each time the button is clicked (followed by accessing the Current property to get at the current state). Don't forget to dispose of the iterator when you've finished with it.

Jon Skeet
A: 

You have to decide what is a "step" in your algorithm. Then you need to rewrite your algorithm and wrap it in a class with the following interface:

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

and now your GUI will drive the algorithm prepared in this way. After you press the button, the NextStep() method will be invoked. If it returns false disable the button (or indicate in whatever other way that its all done). Then read the LastStepResults and update the display.h

Grzenio
+2  A: 
  1. Run your algorithm in a thread different than your UI thread.
  2. For synchronization, create some kind of wait handle, e.g. an AutoResetEvent.
  3. The "pause" statement you are looking for is myWaitHandle.WaitOne() (called by your algorithm thread).
  4. Allow the algorithm to continue by executing myWaitHandle.Set() in your UI thread.

This method has the advantage that your user interface stays responsive while a step of your algorithm is being executed.

Heinzi
A: 

From your description I think you want a "wizard" that is basically an application with previous / next buttons.

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

However If you just have a long running task and want to have some breaks in it, there are different ways to solve it.

  1. Sperate you task in multiple methods.

    After a method is completed, wait until the user hit's next.

  2. Let the task run in it's own thread and at a point where it should wait let the thread sleep until you set a specific var:

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

Set continue1 and 2 to true in your main thread to let the background thread do his work.

SchlaWiener
A: 

If it's just to "observe" the state of the algorithm as it develops, why not add some events (probably just one at the end) and let the event handler store an array of the states. The UI can simply iterate forward\backwards over this as and when needed.

cristobalito