2017-02-26 19:23:31 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.ServiceModel;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2020-04-23 08:16:16 +00:00
|
|
|
|
namespace HeliosPlus.InterProcess
|
2017-02-26 19:23:31 +00:00
|
|
|
|
{
|
|
|
|
|
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
|
|
|
|
|
internal class IPCService : IService
|
|
|
|
|
{
|
|
|
|
|
private static ServiceHost _serviceHost;
|
|
|
|
|
|
|
|
|
|
private IPCService()
|
|
|
|
|
{
|
|
|
|
|
Status = InstanceStatus.Busy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int HoldProcessId { get; set; } = 0;
|
|
|
|
|
|
2017-08-10 14:21:45 +00:00
|
|
|
|
public InstanceStatus Status { get; set; }
|
|
|
|
|
|
2017-02-26 19:23:31 +00:00
|
|
|
|
public void StopHold()
|
|
|
|
|
{
|
|
|
|
|
Application.Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 14:21:45 +00:00
|
|
|
|
public static IPCService GetInstance()
|
|
|
|
|
{
|
2018-10-20 00:27:25 +00:00
|
|
|
|
if (_serviceHost != null || StartService())
|
|
|
|
|
{
|
2017-08-10 14:21:45 +00:00
|
|
|
|
return _serviceHost?.SingletonInstance as IPCService;
|
2018-10-20 00:27:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 14:21:45 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-26 19:23:31 +00:00
|
|
|
|
public static bool StartService()
|
|
|
|
|
{
|
|
|
|
|
if (_serviceHost == null)
|
2018-10-20 00:27:25 +00:00
|
|
|
|
{
|
2017-02-26 19:23:31 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var process = Process.GetCurrentProcess();
|
|
|
|
|
var service = new IPCService();
|
|
|
|
|
_serviceHost = new ServiceHost(
|
|
|
|
|
service,
|
2020-04-29 10:29:43 +00:00
|
|
|
|
new Uri($"net.pipe://localhost/HeliosPlus_IPC{process.Id}"));
|
2017-02-26 19:23:31 +00:00
|
|
|
|
|
|
|
|
|
_serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "Service");
|
|
|
|
|
_serviceHost.Open();
|
2018-10-20 00:27:25 +00:00
|
|
|
|
|
2017-02-26 19:23:31 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_serviceHost?.Close();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2018-10-20 00:27:25 +00:00
|
|
|
|
|
2017-02-26 19:23:31 +00:00
|
|
|
|
_serviceHost = null;
|
|
|
|
|
}
|
2018-10-20 00:27:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-26 19:23:31 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|