Search code examples
c#event-handlingconsole.writelineconsole.readline

Message Handler Stops Seeing Responses?


As a workaround for a dumb issue, I have a separate application do my SharePoint file syncing that I launch as a separate process and communicate via WriteLine and ReadLine. After the third Sync command, it seems the Process executes it (as evidenced by a separate log file), but my SyncToolMessageHandler is no longer getting alerted to its responses even with forced flushes on the external process. If I run the program myself and send the exact same commands, no issue in the console window. So why do I seem to lose my message handler? The Process handle is still alive and well.

I also see this in my debugger on SyncToolProcess. It shows this before I send the first WriteLine so I'm not sure what it's talking about but maybe it's a clue? I'm establishing the process/callbacks and sending commands from an "Update" loop in Unity and the responses are obviously asynchronous whenever the process responds. Is that somehow a problem? enter image description here

Sender:

    public void LaunchSyncTool()
    {
        SyncToolProcess = new Process();
        SyncToolProcess.StartInfo.FileName = "SyncProcess.exe";
        SyncToolProcess.StartInfo.Arguments = SpecialArguments;
        SyncToolProcess.StartInfo.UseShellExecute = false;
        SyncToolProcess.StartInfo.RedirectStandardOutput = true;
        SyncToolProcess.StartInfo.RedirectStandardInput = true;
        SyncToolProcess.StartInfo.RedirectStandardError = true;
        SyncToolProcess.StartInfo.CreateNoWindow = true;
        SyncToolProcess.OutputDataReceived += new DataReceivedEventHandler(SyncToolMessageHandler);
        SyncToolProcess.ErrorDataReceived += new DataReceivedEventHandler(SyncToolMessageHandler);
        SyncToolProcess.Start();
        SyncToolProcess.BeginOutputReadLine();
        SyncToolProcess.BeginErrorReadLine();
        SyncToolProcess.StandardInput.WriteLine("Sync File1 File2");
    }

    // Super simplified, but the ping and pong works great until about the 3rd sync.
    public void SyncToolMessageHandler(object sender, DataReceivedEventArgs e)
    {
        if (e.Data == "Good") 
        {
            Debug("Received 'Good', Sending Command");
            SyncToolProcess.StandardInput.WriteLine("Sync File3 File4");
        }
        else Debug(e.Data); // Exceptions come up Null for some reason, but not seeing them here :-\

        // SyncToolProcess.HasExited is always false
    }

Receiver:

            while (true)
            {
                string inputCmd = await Console.In.ReadLineAsync();
                Debug("Received \"" + inputCmd + "\"");
                try
                {
                    string[] split = inputCmd.Split(' ');
                    switch (split[0])
                    {
                        case "Sync":
                            Sync(split);
                            break;
                        case "Quit":
                            Debug("Quitting");
                            return;
                        default:
                            Debug("Unknown Request: " + inputCmd);
                            break;
                    }
                }
                catch (Exception e)
                {
                    Error(e.Message + "\n" + e.StackTrace);
                }
                Status("Good");
            }
        

Solution

  • Ugh. The problem was the Unity Editor. If I build the program first it no longer misses the callbacks. Not exactly an answer for using the Editor, but at least I know the code is right!