mirror of
https://github.com/ckaczor/WorkIndicator.git
synced 2026-01-13 17:23:18 -05:00
Handle device removal/addition
This commit is contained in:
13
App.xaml.cs
13
App.xaml.cs
@@ -59,22 +59,13 @@ namespace WorkIndicator
|
||||
// Initialize the tray icon
|
||||
TrayIcon.Initialize();
|
||||
|
||||
Task.Factory.StartNew(CheckUpdate).ContinueWith(task => StartApplication(task.Result.Result));
|
||||
}
|
||||
|
||||
private void StartApplication(bool updateRequired)
|
||||
{
|
||||
if (updateRequired)
|
||||
return;
|
||||
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
// Set automatic start into the registry
|
||||
Current.SetStartWithWindows(Settings.Default.StartWithWindows);
|
||||
|
||||
// Initialize the light controller
|
||||
LightController.Initialize();
|
||||
});
|
||||
|
||||
Task.Factory.StartNew(CheckUpdate);
|
||||
}
|
||||
|
||||
private void HandleCommandLine(object sender, InterprocessMessageListener.InterprocessMessageEventArgs e)
|
||||
|
||||
2
Common
2
Common
Submodule Common updated: 81856b2700...4da8f426c0
Submodule Common.Wpf updated: 8a82786166...a71af7de8e
@@ -15,11 +15,15 @@ namespace WorkIndicator
|
||||
public static class LightController
|
||||
{
|
||||
private static StoplightIndicator _stoplightIndicator;
|
||||
private static UsbService _usbService;
|
||||
private static bool _initialized;
|
||||
private static Status _status = Status.Auto;
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
_usbService = new UsbService();
|
||||
_usbService.DevicesChanged += DevicesChanged;
|
||||
|
||||
_stoplightIndicator = new StoplightIndicator();
|
||||
_stoplightIndicator.SetLight(StoplightIndicator.Light.Yellow, StoplightIndicator.LightState.On);
|
||||
|
||||
@@ -29,6 +33,14 @@ namespace WorkIndicator
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
private static void DevicesChanged()
|
||||
{
|
||||
_stoplightIndicator?.Dispose();
|
||||
_stoplightIndicator = new StoplightIndicator();
|
||||
|
||||
UpdateLights();
|
||||
}
|
||||
|
||||
private static void AudioWatcher_MicrophoneInUseChanged(bool microphoneInUse)
|
||||
{
|
||||
UpdateLights();
|
||||
@@ -44,6 +56,8 @@ namespace WorkIndicator
|
||||
_stoplightIndicator.SetLights(StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off);
|
||||
_stoplightIndicator.Dispose();
|
||||
|
||||
_usbService.Dispose();
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
|
||||
52
UsbService.cs
Normal file
52
UsbService.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace WorkIndicator
|
||||
{
|
||||
internal class UsbService : NativeWindow, IDisposable
|
||||
{
|
||||
public event Action DevicesChanged;
|
||||
|
||||
private const int WM_DEVICECHANGE = 0x0219;
|
||||
private const int DBT_DEVICEARRIVAL = 0x8000;
|
||||
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
|
||||
private const int DBT_DEVNODES_CHANGED = 0x0007;
|
||||
|
||||
private bool _isDisposed;
|
||||
|
||||
internal UsbService()
|
||||
{
|
||||
base.CreateHandle(new CreateParams());
|
||||
}
|
||||
|
||||
|
||||
protected override void WndProc(ref Message msg)
|
||||
{
|
||||
base.WndProc(ref msg);
|
||||
|
||||
if (msg.Msg == WM_DEVICECHANGE)
|
||||
{
|
||||
switch (msg.WParam.ToInt32())
|
||||
{
|
||||
case DBT_DEVNODES_CHANGED:
|
||||
case DBT_DEVICEARRIVAL:
|
||||
case DBT_DEVICEREMOVECOMPLETE:
|
||||
DevicesChanged?.Invoke();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
base.DestroyHandle();
|
||||
|
||||
_isDisposed = true;
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,6 +144,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="UpdateCheck.cs" />
|
||||
<Compile Include="UsbService.cs" />
|
||||
<Page Include="App.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
|
||||
Reference in New Issue
Block a user