Solved

.Net Form - Pause and Continue or Restart Loop interruptions

Posted on 2013-05-30
2
338 Views
Last Modified: 2013-05-30
Hello, I have a .Net Form application.
When a "Run" button is clicked it does some Big Loop and constantly updates the GUI.
In case during this loop the user checks a CheckBox
the loop should be halted (so the user can examine the details displayed a TextBox).
Afterwards, the user can click a Continue or Restart button.
How should I program that?
here is a code skeleton.
    //
    foreach (Object o in SomeBigList)
    {
        //
        // do some calculations withe the object and update a TextBox.
        //
        Application.DoEvents();
        if (StopCheckBox.Checked)
        {
            // Pause the Loop and wait for further GUI operation - Continue or Run Again.
        }
    }
    //

private void ContinueButton_Click(object sender, EventArgs e)
{
    // Continue running the Loop.
}

private void RunButton_Click(object sender, EventArgs e)
{
    // If the Loop is Paused then Exit from the Loop.
    // Run again.
}

Open in new window

Thanks, Aryeh.
0
Comment
Question by:tuchfeld
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39209416
Move the loop to a background thread using a BackgroundWorker() control, then pause/resume it using a ManualResetEvent and a boolean flag.
0
 

Author Comment

by:tuchfeld
ID: 39209602
Thanks!
BTW: I also used:
this.Invoke(new MethodInvoker(delegate { textBox2.Text = "hello world"; }));

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 75
SSL https .net web site force redirect 3 48
SQL Exceptions 3 46
Powershell File Sort 8 31
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question