c#和要控制的过程

问题描述 投票:0回答:1

我坚持使用控制日志记录。

private void btPullLinks_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Run_cmd();
        }

        public void Run_cmd()
        {
            StringBuilder outputBuilder;
            ProcessStartInfo processStartInfo;
            Process process;

            outputBuilder = new StringBuilder();

            processStartInfo = new ProcessStartInfo();
            processStartInfo.CreateNoWindow = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.UseShellExecute = false;
            processStartInfo.Arguments = @"c:.......py";
            processStartInfo.FileName = @"...../python.exe";

            process = new Process();
            process.StartInfo = processStartInfo;
            process.EnableRaisingEvents = true;
            process.OutputDataReceived += new DataReceivedEventHandler
            (
                delegate (object sender, DataReceivedEventArgs e)
                {
                    outputBuilder.Append(e.Data);
                    txtoutpute.text = e.Data;
                }
            );

            process.Start();
            process.BeginOutputReadLine();
            process.WaitForExit();
            process.CancelOutputRead();

            // use the output
            outputBuilder.ToString();

        }

如何将输出实时记录到控件中?

我的目标是将数据从委托函数记录到.Net控制。

尝试过此处提供的解决方案。 Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on [duplicate]

c# .net
1个回答
1
投票

我不知道为什么在事件处理程序中,运行时会引发此异常。通常,运行时通过主线程处理调用上下文并执行事件处理程序例程。

https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls

但是如果您无法摆脱它,请尝试(在Program.cs-> Main())

Form.CheckForIllegalCrossThreadCalls = false;
© www.soinside.com 2019 - 2024. All rights reserved.