none
Side effects of a MessageBox? RRS feed

  • Question

  • I'm trying to write a simple application that shows predefined countdown timers.  It has 3 buttons - one for each countdown timer and one to clear it.

    What I want to happen: If the user presses one timer while the other is running, the original timer should stop and be immediately replaced with the second.  In order to allow the user to press other buttons while a timer is running, each is encapsulated in a backgroundworker.

    What is actually happening: If the user presses one timer while the other is running, it stops the running timer and then does precisely bugger all.

    Here's the part I don't get:  If I put a MessageBox into the function that calls the second timer, it works exactly as intended:

    private void buTimer2_Click(object sender, EventArgs e)
            {
                if (bgwTimer1.IsBusy)
                {
                    bgwTimer1.CancelAsync();
                }
                // MessageBox.Show("this makes it work");
                if ((!isRunning) && (!bgwTimer2.IsBusy)) bgwTimer2.RunWorkerAsync();
            }

    Comment out the MessageBox, and it just stops Timer1, leaving its last running time on the display.

    How can I make C# act the way it does with the MessageBox without actually throwing a useless popup at the user?

    Friday, November 9, 2018 6:31 PM

All replies

  • Hello, 

    How can I make C# act the way it does with the MessageBox without actually throwing a useless popup at the user?

    You can't. C# is a language - it didn't act in any way.

    About your task. I think you over complected a solution. You already have handler - buTimer2_Click() - in which you can stop  your timer. There are no needs to put those timers in any wrap.

    I you want a MessageBox to be shown without stopping main thread - run MessageBox from separated thread.


    Sincerely, Highly skilled coding monkey.

    Friday, November 9, 2018 9:03 PM
  • I'm not sure I understand your problem but it seems like you want 3 separate buttons but only 1 timer to be running at any one time. Why not just have a single timer that each of the buttons resets when clicked?

    Posting the code would make this easier to understand by the way.


    Michael Taylor http://www.michaeltaylorp3.net


    Monday, November 12, 2018 3:02 PM
    Moderator