1

我在这里遇到了非常烦人的问题..考虑以下代码:

private static void _KindlePlugedEventWatcher_EventArrived(
        object sender,
        EventArrivedEventArgs e)
    {
        var plugedLogicalDisk = (ManagementBaseObject)e.NewEvent["TargetInstance"];
        if ((plugedLogicalDisk["VolumeName"] as string) == _KindleName)
        {

            var kindleID = plugedLogicalDisk["DeviceID"] as string; // logical letter, like "F:"
            if (kindleID.IsNotNull() &&

                /* somewhy this event fires two times in a sequence,
                thus it is necessary to check,
                if such kindleID is already present 
                in the list of pluged devices;

                because of async sequence Pause() and Resume() 
                are necessary...*/
                !(PlugedDevices.HasItem(
                    (kindleDevice) => { return kindleDevice.ID == kindleID ? true : false; }
                )))

            {
                PauseListening();
                App.Current.Dispatcher.Invoke(
                    async () =>
                    {
                        try
                        {
                            var kindleDevice = await KindleDevice.Create(kindleID);
                            PlugedDevices.Add(kindleDevice);
                        }

                        catch (FileNotFoundException)
                        {
                            /* means that Kindle might not has 
                            'My Clippings.txt' at all,
                            or some IO melfunction happened */

                            throw;
                        }


                    });
                ResumeListening();
            }
        }
    }

两者Pause...()Resume...()只是 MEW 类提供的 Stop()/Start() 方法的简单包装。好吧,那根本行不通。即使我使用 DETACH 处理方法(-= _KindlePlugedEventWatcher_EventArrived)...它会保持第二次射击。此外,我尝试调用 Dispose()。它没有帮助:MEW 对象仍然存在!!!

有人有想法吗?

4

0 回答 0