add try/catch around service check

pull/1154/head
Luke Pulverenti 7 years ago
parent 0fd4b0ffca
commit e5c2e4b28d

@ -196,30 +196,37 @@ namespace MediaBrowser.ServerApplication
private static bool IsAlreadyRunningAsService(string applicationPath) private static bool IsAlreadyRunningAsService(string applicationPath)
{ {
var serviceName = BackgroundService.GetExistingServiceName(); try
{
var serviceName = BackgroundService.GetExistingServiceName();
WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName)); WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName));
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery); ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery);
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get(); ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
foreach (ManagementObject managementObject in managementObjectCollection) foreach (ManagementObject managementObject in managementObjectCollection)
{
var obj = managementObject.GetPropertyValue("PathName");
if (obj == null)
{ {
continue; var obj = managementObject.GetPropertyValue("PathName");
} if (obj == null)
var path = obj.ToString(); {
continue;
}
var path = obj.ToString();
_logger.Info("Service path: {0}", path); _logger.Info("Service path: {0}", path);
// Need to use indexOf instead of equality because the path will have the full service command line // Need to use indexOf instead of equality because the path will have the full service command line
if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1) if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1)
{ {
_logger.Info("The windows service is already running"); _logger.Info("The windows service is already running");
MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service."); MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service.");
return true; return true;
}
} }
} }
catch (COMException)
{
// Catch errors thrown due to WMI not being initialized
}
return false; return false;
} }

Loading…
Cancel
Save