views:

210

answers:

2

Hello

I'm writing an app (.NET Compact Framework 3.5 on PocketPc 2003) I'm trying to detect the cradle event, which is detected but is highly erratic. For instance, on every detection the while loop runs twice. Is this because of multiple registrations to the ActiveSyncEnd event? How do I correct this?

    ///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");
        }
    }
+1  A: 

I have noticed this with Windows CE 5 and ActiveSync. I am not sure if it is an ActiveSync problem. You are going to have to have a flag to indicate it is cradled and ignore other subsequent messages until you detect the removal from the cradle then set the flags back to 0 or false.

0A0D
+1  A: 

Thanks surfrbum... (I thought I was the only one struggling with this.) What you suggested was my previous approach, but it's not very neat.

Anyways, this is how I ultimately managed to solve it:

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;
        }
    }
}

So, follow this cycle: register event, detect the event raised, reset event yourself, unregister event. This way, the cradle detection logic has become very reliable.

Abhijeet Kashnia