当前位置:编程学习 > C#/ASP.NET >>

通过程序实现让手持终端禁止休眠

在手持终端应用程序需要实时连接服务器,而手持设备默认在5分钟后自动跳到休眠状态,虽然可以在设备电源高级选项中设置手持设备从不休眠,但是每次刷机后都需要设置,而且在不运行程序时也是始终不休眠,这样对手持终端的电池的寿命有影响,在这里通过调用系统函数实现使手持终端在开启程序后始终不休眠状态。代码如下:

[csharp]
using System.Runtime.InteropServices; 
using Microsoft.Win32;       
  [DllImport("CoreDll.dll")] 
        private static extern void SystemIdleTimerReset(); 
        private static int nDisableSleepCalls = 0; 
        private static System.Threading.Timer preventSleepTimer = null; 
        private static void PokeDeviceToKeepAwake(object extra) 
        { 
            try 
            { 
                SystemIdleTimerReset();//调用系统函数,使系统不休眠  
            } 
            catch (Exception e) 
            { 
                // TODO  
            } 
        } 
        /**/ 
        /// <summary>  
        /// </summary>  
        public static void DisableDeviceSleep() 
        { 
            nDisableSleepCalls++; 
            if (nDisableSleepCalls == 1) 
            { 
                //Debug.Assert(preventSleepTimer == null);  
                preventSleepTimer = new System.Threading.Timer 
        (new System.Threading.TimerCallback(PokeDeviceToKeepAwake), 
                    null, 0, 30 * 1000); 
            } 
        }        
     private void FrmMain_Load(object sender, EventArgs e) 
        { 
            DisableDeviceSleep();//在程序创建时开启刷新线程        } 

摘自 #Define
 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,