@@ -21,29 +21,33 @@ public class DriveService : IDriveService
2121
2222 public DriveService ( )
2323 {
24- try
24+ // Create in a task to avoid blocking the UI thread. The ManagementEventWatcher can hang.
25+ Task . Run ( ( ) =>
2526 {
26- // Bind to local machine
27- var options = new ConnectionOptions { EnablePrivileges = true } ;
28- var scope = new ManagementScope ( @"root\CIMV2" , options ) ;
29-
30- var query = new WqlEventQuery
27+ try
3128 {
32- EventClassName = "__InstanceModificationEvent" ,
33- WithinInterval = TimeSpan . FromSeconds ( 1 ) ,
34- Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5" // DriveType - 5: CDROM
35- } ;
29+ // Bind to local machine
30+ var options = new ConnectionOptions { EnablePrivileges = true } ;
31+ var scope = new ManagementScope ( @"root\CIMV2" , options ) ;
3632
37- this . watcher = new ManagementEventWatcher ( scope , query ) ;
33+ var query = new WqlEventQuery
34+ {
35+ EventClassName = "__InstanceModificationEvent" ,
36+ WithinInterval = TimeSpan . FromSeconds ( 1 ) ,
37+ Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5" // DriveType - 5: CDROM
38+ } ;
3839
39- // register async. event handler
40- this . watcher . EventArrived += this . HandleDiscEvent ;
41- this . watcher . Start ( ) ;
42- }
43- catch ( Exception e )
44- {
45- System . Diagnostics . Debug . WriteLine ( e . Message ) ;
46- }
40+ this . watcher = new ManagementEventWatcher ( scope , query ) ;
41+
42+ // register async. event handler
43+ this . watcher . EventArrived += this . HandleDiscEvent ;
44+ this . watcher . Start ( ) ;
45+ }
46+ catch ( Exception e )
47+ {
48+ System . Diagnostics . Debug . WriteLine ( e . Message ) ;
49+ }
50+ } ) ;
4751 }
4852
4953 private void HandleDiscEvent ( object sender , EventArrivedEventArgs e )
0 commit comments