微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

消息处理程序停止查看响应?

如何解决消息处理程序停止查看响应?

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

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

enter image description here

发件人:

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

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。