mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-07-25 06:58:40 +02:00
add try/catch around service check
This commit is contained in:
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
|
||||||
|
|
||||||
WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName));
|
|
||||||
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery);
|
|
||||||
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
|
|
||||||
|
|
||||||
foreach (ManagementObject managementObject in managementObjectCollection)
|
|
||||||
{
|
{
|
||||||
var obj = managementObject.GetPropertyValue("PathName");
|
var serviceName = BackgroundService.GetExistingServiceName();
|
||||||
if (obj == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var path = obj.ToString();
|
|
||||||
|
|
||||||
_logger.Info("Service path: {0}", path);
|
WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName));
|
||||||
// Need to use indexOf instead of equality because the path will have the full service command line
|
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery);
|
||||||
if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1)
|
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
|
||||||
|
|
||||||
|
foreach (ManagementObject managementObject in managementObjectCollection)
|
||||||
{
|
{
|
||||||
_logger.Info("The windows service is already running");
|
var obj = managementObject.GetPropertyValue("PathName");
|
||||||
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.");
|
if (obj == null)
|
||||||
return true;
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var path = obj.ToString();
|
||||||
|
|
||||||
|
_logger.Info("Service path: {0}", path);
|
||||||
|
// Need to use indexOf instead of equality because the path will have the full service command line
|
||||||
|
if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1)
|
||||||
|
{
|
||||||
|
_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.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (COMException)
|
||||||
|
{
|
||||||
|
// Catch errors thrown due to WMI not being initialized
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue