C#
public static void Write()
{
try
{
while (true)
{
string local = null;
byte[] bytes;
lock (Gary)
{
local = data + "\n";
}
bytes = Encoding.UTF8.GetBytes(local);
pipeServer.Write(bytes, 0, bytes.Length);
pipeServer.Flush(); // Flush the pipe to ensure all data is sent
Log("Wrote", "write_log.txt");
Thread.Sleep(3); // Sleep for a short time to prevent high CPU usage
}
}
catch (Exception ex)
{
Log($"Error in Write: {ex.Message}", "write_log.txt");
}
}
public static void Read()
{
var sb = new StringBuilder();
while (true)
{
Log("Reading", "read_log.txt");
int b = pipeServer.ReadByte(); // ReadByte blocks until a byte is available, returns -1 if the pipe is closed
Log("Read","read_log.txt");
if (b == -1)
{
Log("Pipe Closed","read_log.txt");
break; // pipe closed
}
if (b == '\n')
{
// Got a full message
string data = sb.ToString().TrimEnd('\r');
var parts = data.Split(',');
lock (Nathaniel)
{
for (int i = 0; i < input.Length && i < parts.Length; i++) input[i] = parts[i] == "1";
}
sb.Clear();
Log("Read full string", "read_log.txt");
}
else
{
sb.Append((char)b);
}
}
}