[英]WIN CE 5.0 ActiveSync Connect/Disconnect?
我不得不将一些软件从 Windows Mobile 6.5 移植到 Windows CE 5.0,该软件当前检测到该单元何时位于基本单元中(ActiveSync 运行)。
我需要知道 ActiveSync 何时在设备上运行,以便准备设备发送和接收文件。
我找到了一篇关于使用 PINVOKE 方法(例如 CeRunAppAtEvent)的文章,但我对它的工作原理一无所知。
bool terminateDeviceEventThreads = false;
IntPtr handleActiveSyncEndEvent;
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, 0);
//
NativeMethods.ResetEvent (handleActiveSyncEndEvent);
if (!NativeMethods.CeRunAppAtEvent ("\\\\.\\Notifications\\NamedEvents\\EventActiveSync",
(int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_NONE))
{
break;
}
handleActiveSyncEndEvent = IntPtr.Zero;
}
}
}
您在此处的代码正在等待系统通知NOTIFICATION_EVENT_RS232_DETECTED
。 通过使用 CeRunAppAtEvent(有点用词不当,因为它不会运行应用程序而是设置事件),他们已经注册了一个名为“EventActiveSync”的命名系统事件,以便在通知发生时设置。
本质上,当设备对接时,命名的系统事件将被设置。
您的代码中有一些等待代码,但不完全 - 它调用 WaitForSingleObject,但从不查看结果然后取消事件。 我认为它看起来更像这样
event EventHandler OnConnect = delegate{};
void ListenerThreadProc()
{
var eventName = "OnConnect";
// create an event to wait on
IntPtr @event = NativeMethods.CreateEvent (IntPtr.Zero, true, false, eventName);
// register for the notification
NativeMethods.CeRunAppAtEvent (
string.Format("\\\\.\\Notifications\\NamedEvents\\{0}", eventName),
(int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_RS232_DETECTED);
while(!m_shutdown)
{
// wait for the event to be set
// use a 1s timeout so we don't prevent thread shutdown
if(NativeMethods.WaitForSingleObject(@event, 1000) == 0)
{
// raise an event
OnConnect(this, EventArgs.Empty);
}
}
// unregister the notification
NativeMethods.CeRunAppAtEvent (
string.Format("\\\\.\\Notifications\\NamedEvents\\{0}", eventName),
(int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_NONE);
// clean up the event handle
NativeMethods.CloseHandle(@event);
}
您的应用程序将创建一个在启动时使用此过程的线程,并为 OnConnect 事件连接一个事件处理程序。
FWIW,SDF 已经完成了这项工作,因此在您的代码中将是这样的:
DeviceManagement.SerialDeviceDetected += DeviceConnected;
...
void DeviceConnected()
{
// handle connection
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.