none
Add a pause, stop, exit button RRS feed

  • Question

  • As a try, I added a pause button to my application that when clicked would show a dialog box.

    The application seems to ignore the click completely and keeps running.

    Is there an example on how to do this?

    Thanks.

    • Moved by CoolDadTx Tuesday, January 14, 2020 2:46 PM Winforms related
    Monday, January 13, 2020 9:56 PM

All replies

  • Hello,

    Best to show the code and the context the code is running under rather than throwing out code samples which may or may not be inline with what you are trying to do.

    And more details are better than less details :-)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, January 13, 2020 11:45 PM
    Moderator
  • This is my sample test code:

    [code]

     bool runWhileLoop = true;
            bool pauseWhileLoop = false;
            bool endWhileLoop = false;

            private void doit()
            {
                while (runWhileLoop == true)
                {
                   
                    if (pauseWhileLoop == true)
                    {
                        DialogResult pause = MessageBox.Show("Some Text", "PAUSED",
       MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                    if (endWhileLoop == true)
                    {
                        runWhileLoop = false;
                    }
                }
            }

            private void startButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.Text = "Loop Started";
                runWhileLoop = true;
                doit();
            }

            private void pauseButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.Text = "Loop Paused";
                pauseWhileLoop = true;
            }

            private void stopButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.Text = "Loop Ended";
                endWhileLoop = true;
            }

    [/code]

    Tuesday, January 14, 2020 5:44 AM
  • Hi jack,

    Thank you for posting here.

    I haven't encountered this kind of error.

    Did you write the code yourself or copied from elsewhere?

    If the code was copied from the web, it may be because the button is not bound to the method.

    If my guess is wrong, please provide the code for us to test.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 7:03 AM
  • Thanks.

    I will try your suggestion in the morning.

    I think I need something like a background worker to access the loop and pause or terminate it as soon as I learn how to do that or see a sample.

    Tuesday, January 14, 2020 7:31 AM
  • Hi jack,

    I took a break at noon and didn't refresh the page afterwards, so I didn't see the code you provided.

    Sorry for that.

    I test the code, it won't run as you expect.

    When the start button is clicked, the program will get stuck in a while loop, causing the UI to freeze.

    Based on your response, you seem to realize this too.

    You can use background worker to solve this problem, or you can use Task to solve the problem.

    Their principles are similar, they all use another thread to execute the method.

           private void startButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.Text = "Loop Started";
                runWhileLoop = true;
                Task task = new Task(() => { doit(); });
                task.Start();
            }


    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 8:21 AM
  • I have revised my sample code with your suggestion as follows:

    <code>

    //-------------------------------------------------------
            // Windows form application
            // has one multiline text box and three buttons
            // using System.Threading.Tasks;
            //-------------------------------------------------------

            bool runWhileLoop = true;
            bool pauseWhileLoop = false;
            bool endWhileLoop = false;

            private void doit()
            {
                while (runWhileLoop == true)
                {               
                    if (pauseWhileLoop == true)
                    {
                        DialogResult pause = MessageBox.Show("Some Text", "PAUSED",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        pauseWhileLoop = false; //continue loop
                        InfoTextBox.AppendText("Loop ReStarted\r\n");  //unsafe call
                    }
                    if (endWhileLoop == true)
                    {
                        runWhileLoop = false; //stop loop
                    }
                }
            }

            private void startButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.AppendText ("Loop Started\r\n");
                runWhileLoop = true;           
                Task task1 = new Task(() => { doit(); });
                task1.Start();           
            }

            private void pauseButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.AppendText("Loop Paused\r\n");
                pauseWhileLoop = true;
                //InfoTextBox.AppendText("Loop ReStarted\r\n");
            }

            private void stopButton_Click(object sender, EventArgs e)
            {
                InfoTextBox.AppendText("Loop Ended\r\n");
                endWhileLoop = true;
                startButton.Enabled = true;
            }

    </code>

    It seems to now run as expected.

    However, I did add a line of code to update the text box to the task and it is generating an exception as an unsafe call. I need to now overcome this as well.

    How would you modify my sample to overcome this or would a background worker be a better choice?

    Tuesday, January 14, 2020 2:20 PM
  • You can use

    SomeTextBox.Invoke((MethodInvoker)(() => SomeTextBox.Text = "whatever")));


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, January 14, 2020 2:46 PM
    Moderator
  • You can use

    SomeTextBox.Invoke((MethodInvoker)(() => SomeTextBox.Text = "whatever")));


    Is that added within the task or before the task is started?

    How does one indicate a code block on this forum?


    EDIT: I figured it out. Thanks. I will do some testing now.
    • Edited by jack0987 Tuesday, January 14, 2020 3:11 PM
    Tuesday, January 14, 2020 2:57 PM
  • Hi jack,

    What was the result of the test? Has the problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    If not, please let us know your current problem.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 15, 2020 9:44 AM