进入睡眠模式的电脑怎样唤醒(休眠模式怎么唤醒电脑)

最近碰到一个关于芯片测试过程中的问题,这颗芯片是用在笔记本端口上,笔记本客户那边会有一个压力测试,就是频繁的电脑电源状态切换,S0(正常使用的开机状态),S3(睡眠模式),S4(休眠模式)以及S5(关机模式)。

当然,主要是客户在压力测试过程中,发现了芯片会不正常的死锁,客户那边将机台寄回来,那么该如何复现呢?客户那边会有自己的一套压力测试系统,不过会测试很多东西,不太方便给我们,而且每一次循环耗时比较久。那么,能不能自己搭建一套控制电脑睡眠,休眠,关机以及唤醒的程序呢?

上面讲的是一个应用背景,告诉大家这其实也是有需求的,只是平时不太用而已,将其记录下来:

首先,从电脑开机状态S0切换到S3,S4甚至是S5,都是比较容易实现的,见下面代码:

Application.SetSuspendState(PowerState.Suspend, false, false);//从S0进入S3
Application.SetSuspendState(PowerState.Hibernate,false,false);//从S0进入S4

Process.Start("shutdown","/s /t 0");    // 参数 /s 的意思是要关闭计算机
                                        // 参数 /t 0 的意思是告诉计算机 0 秒之后执行命令
Process.Start("shutdown", "/r /t 0"); // 参数 /r 的意思是要重新启动计算机

只要调用上述语句即可实现从S0到其他的电源状态,那么反过来唤醒呢?

唤醒的难点在于:当处于S3,S4以及S5的状态下,我的上位机程序是不会运行的,因此,在上位机软件的定时唤醒也是没法工作的。那么笔记本客户那边是怎么操作的呢?他们会通过底层的EC控制来显示上述的功能,可是,我们是不知道底层EC的接口,而且,我们需要一个通用的程式,那要怎么实现呢?

在笔记本的设计中,在S3,S4,S5通常不是所有的东西都会关掉,通常会有一个硬件定时器还在开着,如果我们能操作这个定时器,那是不是就可以实现我们想要的功能呢?

可以调用下面的两个函数,即CreateWaitableTimer以及SetWaitableTimer,这两个函数就可以控制电脑里面开的硬件定时器,当然这个硬件定时器是CPU里面的还是EC里面的,我也不太清楚,没研究过,如果有大神研究过,可以留言,我也学习学习。

[DllImport("kernel32.dll")]
public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);

另外,需要说明的一点是,使用这个定时器也是有条件的,你需要先设置笔记本,"Control Panel > Power Options > Change Plan Settings > Change Advanced Power Settings > Sleep > Allow Wake Timers", 使能定时器唤醒,还有就是,"Control Panel > Power Options > Change Plan Settings > Change Advanced Power Settings > Brad / Additional Settings > Require a password on wakeup",关闭唤醒需要密码。

完成上面的设置,其实已经可以实现电脑从S3,S4,S5唤醒了,但在我使用的过程中,其实还碰到了一个问题,就是唤醒之后,屏幕不亮,你就会误认为没有唤醒,因此我增加了控制鼠标移动的命令,这样,唤醒之后,屏幕就会亮起。

[DllImport("user32.dll")]
public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);

mouse_event(0x0001,0,1,0,UIntPtr.Zero);
mouse_event(0x0001, 0, -1, 0, UIntPtr.Zero);

另外还有一点需要注意,就是笔记本从S0->S3/S4/S5->S0这个循环里面,S0,S3/S4/S5这几个状态的停留时间一定要足够,因为,每个笔记本的完全进入各个状态的时间会不一样,比如,我用我自己的笔记本,这几个状态的停留时间要至少20s,否则,笔记本还没有完全进入就要退出,就会导致,电脑把WaitableTimer关掉,而笔记本还没有唤醒,导致程式死锁。而新的刚买的笔记本,只需要设置10s即可完全进入。

废话不多说,直接上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;

namespace AutoSwitchGUI
{
    public partial class AutoSwitchGUI : Form
    {
        [DllImport("kernel32.dll")]
        public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);

        [DllImport("kernel32.dll")]
        public static extern uint SetThreadExecutionState(uint esFlags);

        [DllImport("user32.dll")]
        public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);

        //public event EventHandler Woken;
        private BackgroundWorker bgWorker = new BackgroundWorker();

        public struct auto_switch_gui_status_t
        {
            public bool test_status;
            public UInt64 test_times_cnt;
            public UInt64 test_times;
            public byte cur_state;

            public int s0_duration;
            public int s3_duration;
        }
        public auto_switch_gui_status_t auto_switch_status;
        public AutoSwitchGUI()
        {
            InitializeComponent();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_Dowork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        }

        private void bgWorker_Dowork(object sender, DoWorkEventArgs e)
        {
            long waketime = (long)e.Argument;
            using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, this.GetType().Assembly.GetName().Name.ToString() + "Timer"))
            {
                if (SetWaitableTimer(handle, ref waketime, 0, IntPtr.Zero, IntPtr.Zero, true))
                {
                    using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
                    {
                        wh.SafeWaitHandle = handle;
                        wh.WaitOne();
                    }
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            mouse_event(0x0001,0,1,0,UIntPtr.Zero);
            mouse_event(0x0001, 0, -1, 0, UIntPtr.Zero);
            auto_switch_status.test_times_cnt++;
            TestTimes.Text = auto_switch_status.test_times_cnt.ToString();
            SystemTimer.Interval = auto_switch_status.s0_duration * 1000;
            SystemTimer.Start();
        }

        public void SetWakeUpTime(UInt64 time)
        {
            bgWorker.RunWorkerAsync(System.DateTime.Now.AddSeconds(time).ToFileTime());
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            try
            {
                auto_switch_status.test_times = UInt64.Parse(SetTestTimes.Text);
                auto_switch_status.s0_duration = int.Parse(S0Duration.Text);
                auto_switch_status.s3_duration = int.Parse(S3Duration.Text);
                if (auto_switch_status.test_times > 0)
                {
                    //SetThreadExecutionState(0x00000001 | 0x00000002 | 0x80000000 | 0x00000040);
                    TestStatus.BackColor = Color.Green;
                    auto_switch_status.test_status = true;
                    TestTimes.Text = "0";
                    auto_switch_status.test_times_cnt = 0;
                    SystemTimer.Interval = auto_switch_status.s0_duration*1000;
                    auto_switch_status.cur_state = 0;
                    SystemTimer.Start();
                    return;
                }
            }
            catch
            {

            }
            MessageBox.Show("Configuration Failed!");
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            SystemTimer.Stop();
            auto_switch_status.test_status = true;
            TestStatus.BackColor = Color.Red;
        }

        private void SystemTimer_Tick(object sender, EventArgs e)
        {
            if (auto_switch_status.cur_state == 0)
            {
                auto_switch_status.cur_state = 0;
                SystemTimer.Stop();
                if (auto_switch_status.test_times_cnt >= auto_switch_status.test_times)
                {
                }
                else
                {
                    SetWakeUpTime((UInt64)auto_switch_status.s3_duration);
                    Application.SetSuspendState(PowerState.Suspend, false, false);
                    //Application.SetSuspendState(PowerState.Hibernate, false, false);
                }
                

            }
            else if (auto_switch_status.cur_state == 1)
            {
                auto_switch_status.test_times_cnt++;
                TestTimes.Text = auto_switch_status.test_times_cnt.ToString();
                auto_switch_status.cur_state = 0;
                
                SendKeys.Send(" ");
                MessageInfo.Text += "TEST1rn";
            }
        }
    }
}

另外声明,关于SetWaitableTimer和CreateWaitableTimer我是参考如下链接的:

进入睡眠模式的电脑怎样唤醒(休眠模式怎么唤醒电脑)

希望可以帮到大家,上面代码在我自己的笔记本以及客户的笔记本是可以适用的。

免责声明: 文章源于会员发布,不作为任何投资建议

如有侵权请联系我们删除,本文链接:https://www.sws100.com/baike/161911.html

(0)
句子百科句子百科
上一篇 2023年 1月 5日 下午3:00
下一篇 2023年 1月 5日 下午4:53

相关推荐

  • 今年和去年比增长率怎么算(今年和去年增长率怎么算)

    新华社北京10月24日电 题:促进重大项目落地 增强经济发展动力——深化投资领域“放管服”改革经验综述 新华社记者王优玲、赵文君 深化“放管服”改革、持续优化营商环境,是稳住经济大盘的内在要求,是激发市场主体活力的重要举措。近年来,各地落实党中央、国务院部署,全方位深化投资领域“放管服”改革,依法依规推进重大项目加快建设,努力释放更多改革红利,增强经济发展动…

    社会百科 2022年 11月 20日
  • 家里客厅放什么绿植比较好风水(家里客厅放什么绿植比较好风水又好养)

    #头号周刊# 随着人们生活水平的提高,人们都喜欢在家里养一些花草绿植,用来美化居室,居家养花,客厅是关键,因为客厅是一家人活动的主要场所,也是接待客人的主要场所,摆放上一盆合适的绿植,能让客厅显得更大气,彰显主人的家居品味,并且客厅的空间相对来说比较大,所以在客厅养花,我们要选择一些独持,大气,寓意好的花卉,可观赏、可“镇家宅”,还能净化空气,让室内的空气更…

    2023年 6月 10日
  • 白百何个人简历(演员白百何个人简历)

    出生日期:1984年3月1日 出 生 地:山东青岛 身 高:172cm 体 重:61公斤 毕业院校:中央戏剧学院表演系本科班 职 业:演员 主要作品:《失联33天》 《与青春有关的日子》等

    2023年 2月 27日
  • 二氧化硫和氢氧化钠反应方程式(二氧化硫和氢氧化钠反应方程式是什么反应)

    随着食品安全被越来越多人重视,我们常看到如“老酸奶是用鞋底做的”一类骇人听闻的新闻标题。这类新闻的实质是:食品工业使得许多化合物被用作食品添加剂。但食帖君认为,不能因涉及相同的化合物,就让原本安全的食品背黑锅啊。 如果按照这样的标题逻辑,我们还可以说:豆奶中有海藻,糖果中有石油~ 1 、无处不在的食品添加剂 维基百科中对食品添加剂的定义是: 为了保持味道或增…

    2023年 2月 22日
  • 抖音小号购买(抖音小号购买平台)

    大家好,最近还有很多朋友对于抖音小号购买不是很理解。然后还有一些网友想弄清楚抖音小号购买平台,句子百科小编(www.sws100.com)已经为你找到了相关问题的答案,接下来和我们一起看看吧,希望对大家有所帮助! 如何开通抖音团购? “抖音来客”App在2022年3月7日正式上线,2022年5月底已经分批次进行迁移,目前在安卓和iOS端已可查找到相关软件。 …

    2022年 10月 3日

联系我们

在线咨询: QQ交谈

邮件:k37759@foxmail.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信