window mobile 防止系统休眠代码

window mobile 过一段时间就会自动休眠,下面的代码可以禁止机器自动休眠。

 

代码
[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 )
            {
                
//  没隔30秒刷新一次计时器
                preventSleepTimer  =   new  System.Threading.Timer( new  System.Threading.TimerCallback

              (PokeDeviceToKeepAwake),
                
null 0 10   *   1000 );
            }
        }

        
///   <summary>
        
///  允许设备自动关闭电源
        
///   </summary>
         public   static   void  EnableDeviceSleep(){
            nDisableSleepCalls
-- ;
            
if  (nDisableSleepCalls  ==   0 ){
                
if  (preventSleepTimer  !=   null ){
                    preventSleepTimer.Dispose();
                    preventSleepTimer 
=   null ;
                }
            }
        }

 

 

 

 

你可能感兴趣的:(mobile)