public void Run()
{
while (true)
{
var security = new PipeSecurity();
security.SetAccessRule(new PipeAccessRule("Administrators", PipeAccessRights.FullControl, AccessControlType.Allow));
using (pipeServer = new NamedPipeServerStream(
Config.PipeName,
PipeDirection.InOut,
NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Message,
PipeOptions.None,
Config.BufferSize, Config.BufferSize,
security,
HandleInheritability.None
))
{
try
{
Console.Write("Waiting...");
pipeServer.WaitForConnection();
Console.WriteLine("...Connected!");
if (THROTTLE_TIMER)
{
timer = new Timer
{
Interval = THROTTLE_DELAY,
};
timer.Elapsed += (sender, args) => SendMessage();
timer.Start();
while(pipeServer.IsConnected)Thread.Sleep(1);
timer.Stop();
}
else
{
while (true) SendMessage();
}
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
if (pipeServer != null)
{
Console.WriteLine("Cleaning up pipe server...");
pipeServer.Disconnect();
pipeServer.Close();
pipeServer = null;
}
}
}
}
}