示例#1
0
            internal override void StopInThread()
            {
                PublishNotificationIterator.Dispose();
                PublishNotificationIterator = null;

                TerminateNotificationIterator.Dispose();
                TerminateNotificationIterator = null;

                Interop.NativeMethods.CFRunLoopRemoveSource(CFRunLoop.Current, NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeDefault);

                NotificationPort.Dispose();
                NotificationPort = null;

                Interop.NativeMethods.CFRunLoopRemoveSource(CFRunLoop.Current, IOConnectionPort.NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeCommon);

                IOConnectionPort.Dispose();
                IOConnectionPort = null;
            }
示例#2
0
            /// <inheritdoc/>
            internal override void StartInThread()
            {
                NotificationPort = new IONotificationPort();
                Interop.NativeMethods.CFRunLoopAddSource(CFRunLoop.Current, NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeDefault);

                var systemPowerDelegate = new IOServiceInterestCallback(SystemPowerInterestCallback);

                IOConnectionPort = IOConnect.CreateSystemPowerMonitorConnection(systemPowerDelegate, this);
                Interop.NativeMethods.CFRunLoopAddSource(CFRunLoop.Current, IOConnectionPort.NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeCommon);

                var servicesDictionary = IOMachPort.GetRS232SerialMatchDictionary();

#if __UNIFIED__
                servicesDictionary.DangerousRetain(); // retain an extra time because we're using it twice
#else
                servicesDictionary.Retain();          // retain an extra time because we're using it twice
#endif // __UNIFIED__
                var publishDelegate = new IONotificationPortCallback(FirstMatchNotification);
                var callback        = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(publishDelegate);

                IntPtr iterator;
                var    result = NativeMethods.IOServiceAddMatchingNotification(NotificationPort.Handle, KIOFirstMatchNotification, servicesDictionary.Handle, callback, IntPtr.Zero, out iterator);
                System.Diagnostics.Debug.Assert(result == NativeMethods.Success, "IOService.IOKitNotificationPort: Failed to add notification.");

                if (result == NativeMethods.Success)
                {
                    PublishNotificationIterator = new IOIterator(iterator);

                    var terminateDelegate = new IONotificationPortCallback(TerminateNotification);
                    callback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(terminateDelegate);
                    result   = NativeMethods.IOServiceAddMatchingNotification(NotificationPort.Handle, KIOTerminatedNotification, servicesDictionary.Handle, callback, IntPtr.Zero, out iterator);
                    TerminateNotificationIterator = new IOIterator(iterator);

                    // The iterators returned when adding the matching notifications must be iterated to
                    // completion in order to arm the notifications. Otherwise, they will never fire.
                    PublishNotificationIterator.EnumerateSerialPorts(null);
                    TerminateNotificationIterator.EnumerateSerialPorts(null);
                }
            }