C#定时器关机,睡眠并重启PC [英] C# timer to shutdown, sleep and restart PC

查看:168
本文介绍了C#定时器关机,睡眠并重启PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按小时,分钟和秒钟的按钮运行= 0



  int  hours =  1 ; 
int minutes = 60 ;
int seconds = 60 ;
private void timer1_Tick( object sender,EventArgs e)
{


if (seconds == 0 && minutes == 0 && hours == 0
{

timer1.Stop();
定时器t =(定时器)发送器;
按钮b = 按钮();
开关(按下)
{
case btnShutdown:Process.Start( shutdown / s / t 0) ; break ;
case btnSleep :Application.SetSuspendState(PowerState.Suspend, false false ); break ;
case btnRestart :Process.Start( shutdown / r / t 0); break ;
}
}

其他 如果(秒== 0
{
分钟 - ;
秒= 59 ;
}
其他 如果(分钟== 0
{
hours--;
分钟= 59 ;
}


秒 - ;
lblSecondes.Text = seconds.ToString();
lbltimeShow.Text =(小时+ + minutes).ToString();
}





我尝试过:



i尝试



 如果 (秒==  0 && minutes ==  0 && hours ==  0 
{

timer1.Stop();
定时器t =(定时器)发送器;
按钮b = 按钮();
开关(按下)
{
case btnShutdown:Process.Start( shutdown / s / t 0) ; break ;
case btnSleep :Application.SetSuspendState(PowerState.Suspend, false false ); break ;
case btnRestart :Process.Start( shutdown / r / t 0); break ;
}
}





但不工作

解决方案

请查看Richard Deeming和我的问题的评论。这个问题没有解释,所以我只能告诉你如何对你的代码进行不可接受的一般设计。它的主要问题是:

魔术字符串 - 维基百科,免费的百科全书 [ ^ ],

另见Anti-pattern - 维基百科,免费的百科全书 [ ^ ]。



要获得一些基本想法,请查看这:

 内部 枚举 SystemSessionAction {关机,睡眠,重启,休眠,锁定,} 

int delay = 0 ;

// ...

Button buttonShutdown = // ...
按钮buttonSleep = // ...
// ...
delay = // ...
// ...

buttonShutdown.Click + =(sender,eventArgs)= > {
EndSystemSession(SystemSessionAction.Shutdown,delay);
};
buttonSleep.Click + =(sender,eventArgs)= > {
EndSystemAction(SystemSessionAction.Sleep,delay);
};

// ...

< span class =code-keyword> void EndSystemAction(SystemSessionAction操作, int 延迟){ / * ... * / }
// 你不必处理延迟,我添加它只是为了说明
// 你可以添加很多动作选项

-SA


嗯,你的意思是时间相等到MidNight(00:00:00)然后app必须执行一个特定的代码?

如果这就是你的意思那么我会在这里检查它。

1.我开始计时器

2.我检查它是否是午夜

3.如果是,那么只需停止计时器并执行你的代码



要检查的代码:

  bool  isMidnight = DateTime.Today.Ticks ==  0 ; 


i want code in buttons run when hours, minutes and seconds=0

int hours = 1;
int minutes = 60;
int seconds = 60;
private void timer1_Tick(object sender, EventArgs e)
{


    if (seconds == 0 && minutes == 0 && hours == 0)
    {

        timer1.Stop();
         Timer t = (Timer)sender;
         Button b = new Button();
         switch (pressed)
         {
             case "btnShutdown": Process.Start("shutdown", "/s /t 0"); break;
             case "btnSleep": Application.SetSuspendState(PowerState.Suspend, false, false); break;
             case "btnRestart": Process.Start("shutdown", "/r /t 0"); break;
         }
    }

    else if (seconds == 0)
    {
        minutes--;
        seconds = 59;
    }
    else if (minutes == 0)
    {
        hours--;
        minutes = 59;
    }


    seconds--;
    lblSecondes.Text = seconds.ToString();
    lbltimeShow.Text = (hours + " : " + minutes).ToString();
}



What I have tried:

i tried

if (seconds == 0 && minutes == 0 && hours == 0)
           {

               timer1.Stop();
                Timer t = (Timer)sender;
                Button b = new Button();
                switch (pressed)
                {
                    case "btnShutdown": Process.Start("shutdown", "/s /t 0"); break;
                    case "btnSleep": Application.SetSuspendState(PowerState.Suspend, false, false); break;
                    case "btnRestart": Process.Start("shutdown", "/r /t 0"); break;
                }
           }



but not work

解决方案

Please see comments to the question by Richard Deeming, and mine. The problem is not explained, so I can only can give you the idea how the unacceptably bad general design of your code. It's major problem is this:
Magic string — Wikipedia, the free encyclopedia[^],
see also Anti-pattern — Wikipedia, the free encyclopedia[^].

To get some basic ideas, please look at this:

internal enum SystemSessionAction { Shutdown, Sleep, Restart, Hibernate, Lock, }

int delay = 0;

// ...

Button buttonShutdown = //...
Button buttonSleep = //...
// ...
delay = // ...
// ...

buttonShutdown.Click += (sender, eventArgs) => {
    EndSystemSession(SystemSessionAction.Shutdown, delay);
};
buttonSleep.Click += (sender, eventArgs) => {
    EndSystemAction(SystemSessionAction.Sleep, delay);
};

// ...

void EndSystemAction(SystemSessionAction action, int delay) { /* ... */ }
// you don't have to deal with delay, I added it just to illustrate that
// you can add many action options

—SA


Hmmm , you mean when the time is equal to MidNight (00:00:00) then app must execute a specific code ?
if that's what you meant then here how I would check it.
1. I start the timer
2. I check if it's midnight or not
3. if it's, then simply stop the timer and execute your code

Code to check :

bool isMidnight = DateTime.Today.Ticks == 0;


这篇关于C#定时器关机,睡眠并重启PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆