DisplayMagician/HeliosPlus/InterProcess/IPCClient.cs

91 lines
2.4 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace HeliosPlus.InterProcess
2017-02-26 19:23:31 +00:00
{
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")))
2017-02-26 19:23:31 +00:00
{
}
2018-10-20 00:27:25 +00:00
public int HoldProcessId
{
get => Channel.HoldProcessId;
}
2017-02-26 19:23:31 +00:00
2018-10-20 00:27:25 +00:00
public InstanceStatus Status
{
get => Channel.Status;
}
2017-08-10 14:21:45 +00:00
2017-02-26 19:23:31 +00:00
public void StopHold()
{
Channel.StopHold();
}
public static IEnumerable<IPCClient> QueryAll()
{
var thisProcess = Process.GetCurrentProcess();
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
foreach (var process in Process.GetProcessesByName(thisProcess.ProcessName))
{
if (process.Id == thisProcess.Id)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
continue;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
IPCClient processChannel = null;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
processChannel = new IPCClient(process);
}
catch
{
// ignored
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (processChannel != null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
yield return processChannel;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
}
public static IPCClient QueryByStatus(InstanceStatus status)
{
var thisProcess = Process.GetCurrentProcess();
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
foreach (var process in Process.GetProcessesByName(thisProcess.ProcessName))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
if (process.Id != thisProcess.Id)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
try
{
var processChannel = new IPCClient(process);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (processChannel.Status == status)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return processChannel;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
catch
{
// ignored
}
2018-10-20 00:27:25 +00:00
}
}
2017-02-26 19:23:31 +00:00
return null;
}
}
}