DisplayMagician/HeliosPlus/InterProcess/IPCService.cs
terrymacdonald 85963b3417 Fixed ShortcutAdaptor errors and Saved prompt
Did a fix for the SHortcutAdaptor doing exceptions
for showing the form before loading all the graphics
but can't really do much about it without adding
background loading to the main form. This is a lot of
work considering we'll be moving from WinForms
to WPF UI in the future.

Also fixed the 'Do you want to save' prompt detection
logic so that it correctly waits until all the loading has
finished before monitoring for users making changes.
Should stop the form incorrectly suggesting you
should save unless they've really made a change.
2020-07-15 20:11:38 +12:00

74 lines
2.1 KiB
C#

using System;
using System.Diagnostics;
using System.ServiceModel;
using System.Windows.Forms;
namespace HeliosPlus.InterProcess
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class IPCService : IService
{
private static ServiceHost _serviceHost;
private IPCService()
{
Status = InstanceStatus.Busy;
}
public int HoldProcessId { get; set; } = 0;
public InstanceStatus Status { get; set; }
public void StopHold()
{
Application.Exit();
}
public static IPCService GetInstance()
{
if (_serviceHost != null || StartService())
{
return _serviceHost?.SingletonInstance as IPCService;
}
return null;
}
public static bool StartService()
{
if (_serviceHost == null)
{
try
{
var process = Process.GetCurrentProcess();
var service = new IPCService();
_serviceHost = new ServiceHost(
service,
new Uri($"net.pipe://localhost/HeliosPlus_IPC{process.Id}"));
_serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "Service");
_serviceHost.Open();
return true;
}
catch (Exception ex)
{
Console.WriteLine($"IPCService/StartService exception: {ex.Message}: {ex.InnerException}");
try
{
_serviceHost?.Close();
}
catch (Exception ex2)
{
Console.WriteLine($"IPCService/StartService exception 2: {ex2.Message}: {ex2.InnerException}");
// ignored
}
_serviceHost = null;
}
}
return false;
}
}
}