DisplayMagician/HeliosDisplayManagement/InterProcess/IPCClient.cs
temacdonald ae1a759be4 [WIP] Changing Icon lib libraries
The current Icon Library is old and I haven't been
using it properly :). I need to use something different
and the IconExtractor library is still being updated
whereas the IconLib.Unofficial isn't any longer.
2020-04-29 22:29:43 +12:00

91 lines
2.4 KiB
C#

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
{
// 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;
}
}
}