DisplayMagician/HeliosDisplayManagement/InterProcess/IPCClient.cs

68 lines
2.1 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 HeliosDisplayManagement.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/HeliosDisplayManagement_IPC{process.Id}/Service")))
{
}
public int HoldProcessId => Channel.HoldProcessId;
2017-08-10 14:21:45 +00:00
public InstanceStatus Status => Channel.Status;
2017-02-26 19:23:31 +00:00
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
{
// 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
{
// ignored
}
return null;
}
}
}