DisplayMagician/HeliosPlus/InterProcess/IPCClient.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

94 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace HeliosPlus.InterProcess
{
internal class IPCClient : ClientBase<IService>, IService
{
public IPCClient(Process process)
: base(
new ServiceEndpoint(
ContractDescription.GetContract(typeof(IService)),
new NetNamedPipeBinding(),
new EndpointAddress(
$"net.pipe://localhost/HeliosPlus_IPC{process.Id}/Service")))
{
}
public int HoldProcessId
{
get => Channel.HoldProcessId;
}
public InstanceStatus Status
{
get => Channel.Status;
}
public void StopHold()
{
Channel.StopHold();
}
public static IEnumerable<IPCClient> QueryAll()
{
var thisProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(thisProcess.ProcessName))
{
if (process.Id == thisProcess.Id)
{
continue;
}
IPCClient processChannel = null;
try
{
processChannel = new IPCClient(process);
}
catch (Exception ex)
{
Console.WriteLine($"IPCClient/QueryAll exception: {ex.Message}: {ex.InnerException}");
// ignored
}
if (processChannel != null)
{
yield return processChannel;
}
}
}
public static IPCClient QueryByStatus(InstanceStatus status)
{
var thisProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(thisProcess.ProcessName))
{
if (process.Id != thisProcess.Id)
{
try
{
var processChannel = new IPCClient(process);
if (processChannel.Status == status)
{
return processChannel;
}
}
catch (Exception ex)
{
Console.WriteLine($"IPCClient/QueryByStatus exception: {ex.Message}: {ex.InnerException}");
// ignored
}
}
}
return null;
}
}
}