C#错误:System.Windows.Forms.dll

时间:2016-07-18 07:59:32

标签: c# visual-studio c#-4.0

每次调试程序并尝试关闭程序然后程序关闭时我都会收到错误,我收到此错误An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

告诉我我的代码有错误。

我的代码:

void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (stop)
        {
            var proc = (Process)sender;

            stop = false; // allows you to spawn a new thread after stopping the first
            proc.SynchronizingObject = this; // puts the form in charge of async communication
            proc.Kill(); // terminates the thread
            proc.WaitForExit(); // thread is killed asynchronously, so this goes here...

        }
        if (e.Data != null)
        {
            string newLine = e.Data.Trim() + Environment.NewLine;
            MethodInvoker append = () => {
                pingInformatsioon.Text += newLine;
                if (checkBox1.Checked)
                {
                    WriteLog(newLine);
                }
            };
            pingInformatsioon.BeginInvoke(append);
        }
    }

总是会在pingInformatsioon.BeginInvoke(append);上面出现黄色,就像有什么不对......

1 个答案:

答案 0 :(得分:1)

您可能正在从UI线程以外的线程调用此代码。尝试替换

pingInformatsioon.BeginInvoke(append);

有这样的东西

if (InvokeRequired)
    pingInformatsioon.BeginInvoke(append);
else
    Invoke(append);