Created
January 4, 2023 22:13
-
-
Save danield137/444e8377e56a0d0788d64ae7749c5e5c to your computer and use it in GitHub Desktop.
Interprocess signaling with C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
namespace TestinManuelResetEventHandler | |
{ | |
internal class Program | |
{ | |
private enum RunMode | |
{ | |
cluster, | |
listen, | |
signal | |
} | |
static CancellationTokenSource cts = new CancellationTokenSource(); | |
static readonly string EventName = "thisismycoolresetevet"; | |
static void Main(string[] args) | |
{ | |
Console.CancelKeyPress += delegate (object? sender, ConsoleCancelEventArgs e) { | |
cts.Cancel(); | |
}; | |
RunMode mode = RunMode.cluster; | |
if (args.Length > 0 && Enum.TryParse<RunMode>(args[0], out var parsed)) | |
{ | |
mode = parsed; | |
} | |
switch (mode) | |
{ | |
case RunMode.cluster: | |
StartCluster(cts.Token); | |
break; | |
case RunMode.listen: | |
StartListening(cts.Token); | |
break; | |
case RunMode.signal: | |
StartSignaling(cts.Token); | |
break; | |
} | |
} | |
private static void StartCluster(CancellationToken token) | |
{ | |
Console.WriteLine($"Running in cluster mode... {Process.GetCurrentProcess().Id}"); | |
var processes = new List<Process>(); | |
processes.AddRange(Enumerable.Range(0, 2).ToList().Select(_ => Process.Start(System.AppDomain.CurrentDomain.FriendlyName, nameof(RunMode.listen)))); | |
Task.Delay(TimeSpan.FromSeconds(1), token).Wait(); | |
processes.Add(Process.Start(AppDomain.CurrentDomain.FriendlyName, nameof(RunMode.signal))); | |
while (!token.IsCancellationRequested) | |
{ | |
Task.Delay(TimeSpan.FromSeconds(1), token); | |
} | |
Console.WriteLine("Shutting down cluster mode..."); | |
processes.ForEach(p => p.Kill()); | |
Environment.Exit(1); | |
} | |
private static void StartSignaling(CancellationToken token) | |
{ | |
Console.WriteLine($"Start signaling on {Process.GetCurrentProcess().Id}"); | |
EventWaitHandle.TryOpenExisting(name: EventName, out var interProcessEventWaitHandler); | |
while (!token.IsCancellationRequested) | |
{ | |
Console.WriteLine($"Singaled on {Process.GetCurrentProcess().Id}"); | |
interProcessEventWaitHandler.Set(); | |
Task.Delay(TimeSpan.FromSeconds(10), token).Wait(); | |
} | |
} | |
private static void StartListening(CancellationToken token) | |
{ | |
var eventHandler = new EventWaitHandle( | |
initialState: false, | |
mode: EventResetMode.ManualReset, | |
name: EventName | |
); | |
Console.WriteLine($"Start listening on {Process.GetCurrentProcess().Id}"); | |
while (!token.IsCancellationRequested) | |
{ | |
eventHandler.WaitOne(); | |
Console.WriteLine($"Recieved on {Process.GetCurrentProcess().Id}"); | |
eventHandler.Reset(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment