Question

My application is set to go to sleep when a notification balloontip is clicked and this process is all looped. This works, but when I awaken my computer, it goes to sleep shortly thereafter (this has a slight time delay, but as this takes as long as it normally takes for my computer to go to sleep, I wouldn't give this any consideration). I've removed this snippet from my overall program, and the program then works perfectly fine, making me think that there's something in this code that makes the computer go to sleep. Furthermore, in case one of the flags controlling the loops is reset, I've reset those flags, but the problem keeps happening. In case (for some strange reason) the boolean operators were impacting it, I've changed them in all the combinations I could, and it still didn't work! I've tried adding in a command to prevent the computer from going to sleep, but that didn't work either. Any suggestions? Much appreciated.

for (; ; )
{
    bool for3 = false;
    for (; ; )
    {
        notifyIcon.ShowBalloonTip(10000);
        System.Threading.Thread.Sleep(10000);
        Application.DoEvents();
        if (loopVariable)
            for3 = true;
        if (for3) break;
        System.Threading.Thread.Sleep(60000);
    }
    loopVariable = false;
    for3 = false;
    DateTime timeOnSleep = DateTime.Now;
    Application.SetSuspendState(PowerState.Suspend, false, false);//this is the annoying code
        DateTime timeOnWake = DateTime.Now;
        TimeSpan time = timeOnWake - timeOnSleep;
        var minutes = time.TotalMinutes;
        Math.Round(time.TotalMinutes, MidpointRounding.AwayFromZero);
        double dMins = minutes;
        try
        {
            int iNumber = System.Convert.ToInt32(dMins);
        }
        catch (System.OverflowException)
        {
            if (dMins >= 40)
                break;
        }
    }
private static bool loopVariable = false;

void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
    loopVariable = true;
}
Était-ce utile?

La solution

If you find yourself using doevents, you're likely doing something wrong. If you needed to run a loop like that use do {} rather than for (;;). In any case, you don't need either of those things. If this isn't supposed to run infinitely, you can disable the timer just before calling Application.SetSuspendState.

    void Main()
    { 
        Timer timer = new Timer();
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = 30000; //How often you want to show the tooltip?
        timer.Enabled = true;
    }

    void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
    {
        Application.SetSuspendState(PowerState.Suspend, false, false);
    }

    void timer_Tick(object sender, EventArgs e)
    {
        notifyIcon1.ShowBalloonTip(10000);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top