简体   繁体   中英

Spamming Message Boxes c#

I am making a program that runs in the background and at specific times of the day it will pop up a window with instructions for exercise. (For office workers at my job)

The problem is: When the time comes for the exercise instructions I want a message box to pop up and ask if you are ready. I have this message box code inside a "if(blabla == true)" so it keeps spamming.

How do I avoid this? I can't see any other place to put the message box.

It's basically:

if (DateTime.Now.ToString("HH:mm") == "09:00")
{
    progress = true;
}

if(progress == true)
{
    DialogResult dialogResult = MessageBox.Show("Start?", "Bla Bla",      MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
    exercise.Action();
}
}

Is there a way to have the Dialog at the same place but have it appear once? Would make my project so much easier.

Thanks!

It's not a C# or WinForms problem, it's an algorithm problem. Try to design the algorithm first, then translate it into the programming language of your choice.

What about the following (pseudocode, assuming that you want to interrupt your people every full hour):

nextHour = 9

loop forever:
    if now = nextHour:00
        show message box
        if answer = yes
            exercise
        nextHour++
    else
        sleep for a minute

This will increase nextHour from 9 to 10 after showing the message box exactly once.


There is, in fact, another option: What you are trying to implement is a scheduler , a program that performs a certain action at a certain time. Windows already contains a scheduler, the Windows Task Scheduler .

Thus, instead of re-inventing the wheel, you could make your program look like this:

show message box
if answer = yes
    exercise

compile it into an exe and set the Windows Task Scheduler to run it every hour (or whatever frequency your user desires).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM