0

作为一个愚蠢问题的解决方法,我有一个单独的应用程序来同步我的 SharePoint 文件,我将它作为一个单独的进程启动并通过 WriteLine 和 ReadLine 进行通信。在第三个 Sync 命令之后,似乎进程执行它(由单独的日志文件证明),但即使在外部进程上强制刷新,我的 SyncToolMessageHandler 也不再收到其响应的警报。如果我自己运行程序并发送完全相同的命令,则控制台窗口中没有问题。那么为什么我似乎丢失了我的消息处理程序?Process 句柄仍然存在并且运行良好。

我也在 SyncToolProcess 上的调试器中看到了这一点。它在我发送第一个 WriteLine 之前显示了这一点,所以我不确定它在说什么,但也许这是一个线索?我正在建立流程/回调并从 Unity 中的“更新”循环发送命令,并且每当流程响应时响应显然是异步的。这在某种程度上是个问题吗? 在此处输入图像描述

发件人:

    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
    }

接收者:

            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");
            }
        
4

1 回答 1

0

啊。问题是 Unity 编辑器。如果我先构建程序,它不再错过回调。不完全是使用编辑器的答案,但至少我知道代码是正确的!

于 2021-03-25T21:57:38.993 回答