private static IntPtr DeviceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_DEVICECHANGE) { switch (wParam.ToInt32()) { case WM_DEVICECHANGE_EVENT.DBT_DEVICEARRIVAL: case WM_DEVICECHANGE_EVENT.DBT_DEVICEREMOVECOMPLETE: if (_cancelRestart != null) { _cancelRestart.Cancel(); } _cancelRestart = new CancellationTokenSource(); Task.Delay(TimeSpan.FromSeconds(1), _cancelRestart.Token).ContinueWith(_ => { if (_.IsCanceled) { return; } App.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() => { Sidebar _sidebar = App.Current.Sidebar; if (_sidebar != null) { _sidebar.ContentReload(); } })); _cancelRestart = null; }); break; } handled = true; } return(IntPtr.Zero); }