Closed spark6-dev closed 3 years ago
then initialize the functionality inside the OnStart event
These are not exact steps to reproduce the problem.
Please provide exact code to reproduce the problem.
try
{
EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service start successfully." });
using (IUsbEventWatcher usbEventWatcher = new UsbEventWatcher())
{
EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { usbEventWatcher.UsbDeviceList.FirstOrDefault().DeviceName.ToString() });
Console.WriteLine(usbEventWatcher.UsbDeviceList.FirstOrDefault());
usbEventWatcher.UsbDeviceRemoved += (_, device) => EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Removed:" + Environment.NewLine + device + Environment.NewLine });
usbEventWatcher.UsbDeviceAdded += (_, device) => EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Added:" + Environment.NewLine + device + Environment.NewLine });
usbEventWatcher.UsbDriveEjected += (_, path) => EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Ejected:" + Environment.NewLine + path + Environment.NewLine });
usbEventWatcher.UsbDriveMounted += (_, path) =>
{
Console.WriteLine("Mounted:" + Environment.NewLine + path + Environment.NewLine);
EventLog.WriteEvent("Mounted", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Ejected:" + Environment.NewLine + path + Environment.NewLine });
foreach (string entry in Directory.GetFileSystemEntries(path))
EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Entries:" + Environment.NewLine + entry + Environment.NewLine });
};
}
}
catch (Exception ex)
{
EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { ex.Message });
}
IUsbEventWatcher usbEventWatcher
starts listening for USB events from the moment you call usbEventWatcher = new UsbEventWatcher()
and it stops listening for USB events when you call usbEventWatcher.Dispose()
using (IUsbEventWatcher usbEventWatcher = new UsbEventWatcher())
calls usbEventWatcher.Dispose()
when the execution reaches }
because that is how using
works:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
solution:
IUsbEventWatcher usbEventWatcher
a private member of the Windows service classusbEventWatcher = new UsbEventWatcher()
inside OnStart
- without using
usbEventWatcher.Dispose()
inside OnStop
@Jinjinov: Your efforts are greatly appreciated and thank you for providing some valuable solutions on time. It worked great and your quick response was really appreciated.
Library version
10.0.0.0
OS & OS version
Windows 10 Pro & 21H1
Describe the bug
Inside the windows service it does not trigger any events with this package.
When we tested this package, it worked in the console application. But this does not work within the Windows services. Is there are any restrictions for this to work inside the Windows services?
To Reproduce