首页 > 解决方案 > 将 Process.StandardInput 与 Mono 一起使用

问题描述

我正在尝试将现有的 Windows 窗体应用程序转换为使用 Mono 在 Mac 上运行。在大多数情况下,整个应用程序都按预期运行,但有一个例外。该应用程序的一部分使用 Graphviz 绘制输出并生成 PDF,这在 Windows 上运行良好。但是,当我在 Mac 上使用 Mono 运行时,我得到“System.IO.IOException:路径 /Users/Kevin/Desktop/TitleChain/[Unknown] 上的写入错误”(下面的完整堆栈跟踪)。我认为,我已经追溯到这样一个事实,即 Mono 启动进程并在我可以执行 process.StandardInput.WriteLine 发送点文件的文本之前立即退出。不确定我是否可以在 ProcessStartInfo 中包含一些内容,以防止进程过早退出。也许使用 ProcessStartInfo.RedirectStandardInput/Output 的东西?

该代码可以毫无问题地找到 dot.exe Graphviz 可执行文件,并且我已经克服了权限问题。一切都表明 Mono 在我处理 StandardInput 和 StandardOutput 时没有意识到进程需要保持打开状态。

为了在信用到期时给予信用,此代码最初改编自 graphvizwrapper:https ://github.com/JamieDixon/GraphViz-C-Sharp-Wrapper

        var processStartInfo = this.GetProcessStartInfo(fileType);
        if (masterForm.IsRunningOnMono() || debugMode)
        {
            Console.WriteLine("Running Mono, using alternative process methodology");                

            var process = Process.Start(processStartInfo);
            process.StandardInput.WriteLine(dotFile);
            var baseStream = process.StandardOutput.BaseStream;

            output = this.ReadFully(baseStream);
        }

使用的功能:

    private System.Diagnostics.ProcessStartInfo GetProcessStartInfo(string returnType)
    {
        return this.getProcessStartInfoQuery.Invoke(new ProcessStartInfoWrapper
        {
            FileName = this.FilePath,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            Arguments = "-v -o -Tpdf",
            CreateNoWindow = true
        });
    }

    private byte[] ReadFully(Stream input)
    {
        using (var ms = new MemoryStream())
        {
            input.CopyTo(ms);
            return ms.ToArray();
        }
    }

GetStartProcessQuery 类:

    public Process Invoke(ProcessStartInfo processStartInfo)
    {
        return Process.Start(processStartInfo);
    }

我能想到的唯一其他相关信息是初始应用程序位于 TitleChain 文件夹中并从其执行,graphviz dot.exe 位于名为“graphviz”的子目录中。

提前感谢您的帮助。

堆栈跟踪:

System.IO.IOException: Write fault on path /Users/Kevin/Desktop/TitleChain/[Unknown]

at System.IO.FileStream.WriteInternal (System.Byte[] src, System.Int32 offset, System.Int32 count) [0x00077] in <bb7b695b8c6246b3ac1646577aea7650>:0 

at System.IO.FileStream.Write (System.Byte[] array, System.Int32 offset, System.Int32 count) [0x00090] in <bb7b695b8c6246b3ac1646577aea7650>:0 

at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder) [0x0007e] in <bb7b695b8c6246b3ac1646577aea7650>:0 

at System.IO.StreamWriter.Write (System.Char[] buffer, System.Int32 index, System.Int32 count) [0x000d3] in <bb7b695b8c6246b3ac1646577aea7650>:0 

at System.IO.TextWriter.WriteLine (System.String value) [0x00070] in <bb7b695b8c6246b3ac1646577aea7650>:0 

at TitleChainApplication.GraphVizWrapper.GraphGeneration.GenerateGraph (System.String dotFile, TitleChainApplication.Enums+GraphReturnType returnType) [0x00103] in <5bfb1715358f4c24a3905e8d8ae55245>:0 

at TitleChainApplication.Form1.ExportTree (TitleChainApplication.Enums+GraphReturnType fileType) [0x00034] in <5bfb1715358f4c24a3905e8d8ae55245>:0 

at TitleChainApplication.Form1.ExportPDF () [0x00001] in <5bfb1715358f4c24a3905e8d8ae55245>:0 

at TitleChainApplication.Form1.pDFToolStripMenuItem_Click (System.Object sender, System.EventArgs e) [0x00001] in <5bfb1715358f4c24a3905e8d8ae55245>:0 

at TitleChainApplication.Form1.Export_ButtonClick (System.Object sender, System.EventArgs e) [0x00001] in <5bfb1715358f4c24a3905e8d8ae55245>:0 

at System.Windows.Forms.ToolStripSplitButton.OnButtonClick (System.EventArgs e) [0x00019] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.ToolStripSplitButton.HandleClick (System.Int32 mouse_clicks, System.EventArgs e) [0x00028] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs e, System.Windows.Forms.ToolStripItemEventType met) [0x00054] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at (wrapper remoting-invoke-with-check) System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs,System.Windows.Forms.ToolStripItemEventType)

at System.Windows.Forms.ToolStrip.OnMouseUp (System.Windows.Forms.MouseEventArgs mea) [0x00048] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00078] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x001b4] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message& m) [0x00000] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.ToolStrip.WndProc (System.Windows.Forms.Message& m) [0x00000] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x0000b] in <84cef96c1ef54299a622b3e52fada5d0>:0 

at System.Windows.Forms.NativeWindow.WndProc (System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) [0x00085] in <84cef96c1ef54299a622b3e52fada5d0>:0

标签: c#processmonographvizdot

解决方案


推荐阅读