ManagementEventWatcher停止/启动/处置问题

时间:2016-01-15 10:41:41

标签: c# wmi managementeventwatcher

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

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)...... IT KEEPS第二次发射。而且,我试过调用Dispose()。它没有帮助:MEW对象仍然生活!

有人有想法吗?

0 个答案:

没有答案