Resolved Timed Message Box duplicating Message Box

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I have a message box show that I wanted to pop up at a given time.

I have set up the code below, but it fires like 9 times and I get 9 message boxes - all the same. I assume this was due to the ticks being on the second :00 for 9 ticks (given that my clock interval is set to 100) - so I tried to work in an interval break

C#:
System.Timers.Timer t = new System.Timers.Timer();

t.Interval = 2000;
t.Start();
t.Stop();

That didn't work, so tried a bool that I could change once the message box had shown, but that didn't work either.

As you can imagine, trying to debug this using breakpoints is pretty tough, I have to change my PC clock each time and of course, the moment it stops for the break point, the next tick is several seconds later and bypasses the code entirely as it isn't 23:55:00 anymore...

I just wondered if anyone had a neat workaround for this type of thing a way to fire off the message box just the once on the tick.

Code for the bool is below but as I say, it doesn't work.

C#:
bool isOpen = false;

            if (time == "23:55:00")
            {
                if (isOpen == false)
                {
                    if (File.Exists(EX1))
                    {
                        MessageBox.Show($"At 11:59pm the application will delete ALL data files, including the excel {EX1}");
                        isOpen = true;
                    }
                    else
                    {
                        MessageBox.Show("At 11:59pm the application will create an Excel.");
                        isOpen = true;
                    }
                }
                else
                {

                }
            }

Just so you know, I tried the following as well:

C#:
if (time == "23:55:00" && isOpen == false)
{
    //same code as above
}

That didn't work either.
 
Last edited:
I didn't think to set a different time - but when I tried it...it didn't work.

I changed "23:55:00" to "17:47:00" (and through to about 17:59:00 before resetting it to 23:55:00 changing my PC clock to test and realising it does work - and it just skips right over it on 17:55:00 for example....that is with exactly the same code and just changing those two numbers - I have absolutely no idea why it would do that....

I have also changed my PC clock to "18:03:00" and tested it, just in case using the change clock method created a different outcome that the app was seeing but wouldn't see normally....nope just ignored it - changed it back to 23:55:00 - yep fires off. That is just weird.

For the record, I have added no other code yet....

Looking into the AutoRest Property now - I think I will struggle a little to implement it - as my timer is in a different method and it will be a pain to draw in - but I think I can work it out.

Thanks for the heads up.
 
Last edited:
For anyone that cares, I had a bit of a run around with this and changed the IF statement to the clock text....didn't help the message box creation being 9, but it did mean that I could adjust the time and it would recognise it - so made testing easier.

Also took JMC's advice from a previous thread and built the code in a new project - where it worked....not sure why it works there but not here...but regardless it helped me get to the code I needed...

C#:
if (clock1.Text == "00:25:00")
            {
                t.Stop();
                if (File.Exists(EX1))
                {
                    MessageBox.Show("At 11:59pm the application will delete ALL data files.");
                }
                else
                {
                    MessageBox.Show("At 11:59pm the application will create an Excel for today's shift, it will delete old data files and create new data files on completion.  None of your work will be lost.");
                }
                t.Start();
            }

t being the really good name I gave the timer...

It does stop the clock until the individual presses ok - but that isn't important in this application.
 
Back
Top Bottom