1

[DllImport("CoreDll.dll")]
2

private static extern void SystemIdleTimerReset();
3

4

private static int nDisableSleepCalls = 0;
5

private static System.Threading.Timer preventSleepTimer = null;
6

7

private static void PokeDeviceToKeepAwake(object extra)
8


{
9

try
10


{
11

SystemIdleTimerReset();
12

}
13

catch (Exception e)
14


{
15

// TODO
16

}
17

}
18
19


/**//// <summary>
20

/// 禁止设备自动关闭电源
21

/// </summary>
22

public static void DisableDeviceSleep()
23


{
24

nDisableSleepCalls++;
25

if (nDisableSleepCalls == 1)
26


{
27

//Debug.Assert(preventSleepTimer == null);
28

// 没隔30秒刷新一次计时器
29

preventSleepTimer = new System.Threading.Timer(new System.Threading.TimerCallback(PokeDeviceToKeepAwake),
30

null, 0, 30 * 1000);
31

}
32

}
33


/**//// <summary>
34

/// 允许设备自动关闭电源
35

/// </summary>
36

public static void EnableDeviceSleep()
37


{
38

nDisableSleepCalls--;
39

if (nDisableSleepCalls == 0)
40


{
41

//Debug.Assert(preventSleepTimer != null);
42

if (preventSleepTimer != null)
43


{
44

preventSleepTimer.Dispose();
45

preventSleepTimer = null;
46

}
47

}
48

}
49

这样我们只要在同步前调用DisableDeviceSleep方法,在同步后调用EnableDeviceSleep就OK了, 在也不用担心用户PDA电源自动关闭引起同步失败了.
可是事情远比我们想象的要复杂, 大家都PDA的背光也会自动关闭, 你可以想象一用户在同步时候的情形, 他在等待漫长的同步过程时背光突然关闭了, 可是我们让他电源不断, 这时候他以为电源关闭了, 结果按下了电源键盘, 结果不用我说了吧.
背光我们同样要处理, 是否也是有个线程作为背光定时器内, 答案是肯定的, 不过为了强调我们研究的技术性, 背光我用修改WinCE注册表方式搞定. 通过查阅资料我找到了这四个注册表键:
[HKEY_CURRENT_USER\ControlPanel\Backlight\BatteryTimeout] 使用外接电源时PDA自动关闭背光时间
[HKEY_CURRENT_USER\ControlPanel\Backlight\BatteryTimeoutUnchecked] 禁止使用电池自动关闭时为选中的时间
[HKEY_CURRENT_USER\ControlPanel\Backlight\ACTimeoutUnchecked] 禁止使用外接电源自动关闭时为选中的时间


/**//// <summary>

/// 指示背光是否被设置

/// </summary>

private static int nBatteryTimeoutCalls = 0;

private static int nACTimeoutCalls = 0;


/**//// <summary>

/// 禁止背光自动关闭

/// </summary>

public static bool DisableBacklightOff()


{

OpenNETCF.Win32.RegistryKey key = null;

bool bIsSet = false;

try


{

key = OpenNETCF.Win32.Registry.CurrentUser.OpenSubKey(@"ControlPanel\Backlight\", true);

if(key != null)


{

string oldValue1 = String.Empty;

string oldValue2 = String.Empty;

oldValue1 = key.GetValue("BatteryTimeout").ToString();

oldValue2 = key.GetValue("ACTimeout").ToString();

if(oldValue1.Trim() != "0")


{

nBatteryTimeoutCalls++;

if(nBatteryTimeoutCalls == 1)


{

key.SetValue("BatteryTimeout", 0);

key.SetValue("BatteryTimeoutUnchecked", int.Parse(oldValue1));

bIsSet = true;

}

else


{

nBatteryTimeoutCalls--;

}

}

if(oldValue2.Trim() != "0")


{

nACTimeoutCalls++;

if(nACTimeoutCalls == 1)


{

key.SetValue("ACTimeout", 0);

key.SetValue("ACTimeoutUnchecked", int.Parse(oldValue2));

bIsSet = true;

}

else


{

nACTimeoutCalls--;

}

}

if(bIsSet)


{

IntPtr hBackLightEvent = CreateEvent(IntPtr.Zero, false, true, "BackLightChangeEvent");

if (hBackLightEvent != IntPtr.Zero)


{

EventModify(hBackLightEvent,3);

CloseHandle(hBackLightEvent);

}

}

}

return true;

}

catch


{

return false;

}

finally


{

if(key != null)


{

key.Dispose();

key = null;

}

}

}


/**//// <summary>

/// 允许背光自动关闭

/// </summary>

public static bool EnableBacklightOff()


{

OpenNETCF.Win32.RegistryKey key = null;

bool bIsSet = false;

try


{

key = OpenNETCF.Win32.Registry.CurrentUser.OpenSubKey(@"ControlPanel\Backlight\", true);

if(key != null)


{

string oldValue1 = String.Empty;

string oldValue2 = String.Empty;

oldValue1 = key.GetValue("BatteryTimeoutUnchecked").ToString();

oldValue2 = key.GetValue("ACTimeoutUnchecked").ToString();

if(oldValue1.Trim() != "0")


{

nBatteryTimeoutCalls--;

if(nBatteryTimeoutCalls == 0)


{

key.SetValue("BatteryTimeoutUnchecked", 0);

key.SetValue("BatteryTimeout", int.Parse(oldValue1));

bIsSet = true;

}

else


{

nBatteryTimeoutCalls++;

}

}

if(oldValue2.Trim() != "0")


{

nACTimeoutCalls--;

if(nACTimeoutCalls == 0)


{

key.SetValue("ACTimeoutUnchecked", 0);

key.SetValue("ACTimeout", int.Parse(oldValue2));

bIsSet = true;

}

else


{

nACTimeoutCalls++;

}

}

if(bIsSet)


{

IntPtr hBackLightEvent = CreateEvent(IntPtr.Zero, false, true, "BackLightChangeEvent");

if (hBackLightEvent != IntPtr.Zero)


{

EventModify(hBackLightEvent,3);

CloseHandle(hBackLightEvent);

}

}

}

return true;

}

catch


{

return false;

}

finally


{

if(key != null)


{

key.Dispose();

key = null;

}

}

}



OK! 问题解决.