DisplayMagician/HeliosPlus/InterProcess/IPCClient.cs
terrymacdonald 5004f5f76a Silenced the System.Drawing.Image exceptions
The ProfileAdapter and ShortcutAdapters used
by the ImageListView Control unfortunately
have to access the sizes of the bitmaps being
loaded into the respective imagelistviews. I
can't find anyway of stopping the GDI+
from complaining about the Bitmap being
accessed by multiple different threads (as
ImageListView creates one thread per image.
This will be fixed once I move to this being a
WPF application as we'll use a different control.
2020-07-24 16:51:48 +12:00

94 lines
2.7 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.StackTrace} - {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.StackTrace} - {ex.InnerException}");
// ignored
}
}
}
return null;
}
}
}