DisplayMagician/HeliosDisplayManagement/InterProcess/IPCService.cs
temacdonald a9bb295d1f Renamed app to HeliosPlus namespace and more
Renamed app to HeliosPlus namespace so that the updated
changes don't interfere with HeliosDisplayManagement if
that is also installed. The fact that I've changed so much of
the app means that my changes would be unlikely to be
accepted by Soroush, so I'm best to release this work in a
similar way to other projects like Notepad++, by keeping
the same root name, and adding a plus.
    I've also changed the Shortcut form to put all the games
in a single list to reduce the number of clicks a user has to
do in order for them to create a shortcut. I have begun to
prepare the form so that it will support multiple game
libraries, but now I am at a point that I need to fix the Steam
and Uplay game detection mechanisms so that they report
the correct information for the lv_games list view.
2020-04-23 20:16:16 +12:00

72 lines
1.8 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/HeliosDisplayManagement_IPC{process.Id}"));
_serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "Service");
_serviceHost.Open();
return true;
}
catch (Exception)
{
try
{
_serviceHost?.Close();
}
catch
{
// ignored
}
_serviceHost = null;
}
}
return false;
}
}
}