捕获ActiveSyncEnd事件:

时间:2009-10-14 16:16:55

标签: c# compact-framework activesync

我正在编写一个应用程序(PocketPc 2003上的.NET Compact Framework 3.5) 我正在尝试检测摇篮事件,该事件被检测到但是非常不稳定。 例如,在每次检测时,while循环运行两次。 这是因为多次注册ActiveSyncEnd事件? 我该如何纠正?

    ///NOTIFICATION_EVENT_NONE                =  0,
    ///NOTIFICATION_EVENT_TIME_CHANGE         =  1,
    ///NOTIFICATION_EVENT_SYNC_END            =  2,
    ///NOTIFICATION_EVENT_ON_AC_POWER         =  3,
    ///NOTIFICATION_EVENT_OFF_AC_POWER        =  4,
    ///NOTIFICATION_EVENT_NET_CONNECT         =  5,
    ///NOTIFICATION_EVENT_NET_DISCONNECT      =  6,
    ///NOTIFICATION_EVENT_DEVICE_CHANGE       =  7,
    ///NOTIFICATION_EVENT_IR_DISCOVERED       =  8,
    ///NOTIFICATION_EVENT_RS232_DETECTED      =  9,
    ///NOTIFICATION_EVENT_RESTORE_END         = 10,
    ///NOTIFICATION_EVENT_WAKEUP              = 11,
    ///NOTIFICATION_EVENT_TZ_CHANGE           = 12,
    ///NOTIFICATION_EVENT_MACHINE_NAME_CHANGE = 13      

// In DeviceEventManager       
public void ActiveSyncEndDetect()
{
    try
    {
        // Put 9 for cradle event, 2 for ActiveSyncEnd event, 0 for none. 
        handleActiveSyncEndEvent = NativeMethods.CreateEvent(IntPtr.Zero, false, false, "EventActiveSync");                
        while (!terminateDeviceEventThreads)
        {
            //NativeMethods.CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\EventActiveSync", 2);
            //NativeMethods.CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\EventActiveSync", 9);
            NativeMethods.CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\EventActiveSync", 2);                    
            NativeMethods.WaitForSingleObject(handleActiveSyncEndEvent, 0xFFFFFFFF);

            //MessageBox.Show("Activesync ended.");                
            //Do something here

            NativeMethods.CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\EventActiveSync", 0);
            }
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show("Exception in ActiveSyncEndDetect method");
        }
    }

2 个答案:

答案 0 :(得分:2)

谢谢surfrbum ......(我以为我是唯一一个苦苦挣扎的人。) 你的建议是我以前的方法,但它不是很整洁。

无论如何,这是我最终设法解决它的方式:

while (!terminateDeviceEventThreads)
{
    handleActiveSyncEndEvent = NativeMethods.CreateEvent(IntPtr.Zero,
                                        true, false, "EventActiveSync");
    if (IntPtr.Zero != handleActiveSyncEndEvent)
    {
        if (NativeMethods.CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\EventActiveSync",
                     (int)NOTIFICATION_EVENT.NOTIFICATION_EVENT_RS232_DETECTED))
        {
            NativeMethods.WaitForSingleObject(handleActiveSyncEndEvent, 0xFFFFFFFF);
            if (activeSyncEndDelegateInstance != null)
            {
                OnActiveSyncEnd();
            }
            ResetEvent(handleActiveSyncEndEvent);
            if (!NativeMethods.CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\EventActiveSync",
                             (int)NOTIFICATION_EVENT.NOTIFICATION_EVENT_NONE))
            {
                break;
            }
            handleActiveSyncEndEvent = IntPtr.Zero;
        }
    }
}

因此,请遵循以下周期:注册事件,检测引发的事件,自行重置事件,取消注册事件。 这样,支架检测逻辑变得非常可靠。

答案 1 :(得分:1)

我已经注意到Windows CE 5和ActiveSync。我不确定它是否是ActiveSync问题。您将不得不有一个标志来指示它已被搁置并忽略其他后续消息,直到您检测到从支架中移除然后将标志设置回0或false。