InvalidOperationException的未知原因?

时间:2020-06-22 15:48:01

标签: c# wpf

我有一个字符串数组,可用于拆分另一个进程的输出。然后,我想在WPF界面的文本框中显示该数组的第一个元素。但是,当我尝试这样做时会收到此异常,有关如何解决该异常的任何想法?

        output = p.StandardOutput.ReadToEnd();
        code = p.ExitCode;
        p.WaitForExit();
        string[] result = this.output.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        this.outputList.Add(Convert.ToDouble(result[0]));
        this.MyTextBox.Text = result[0];

1 个答案:

答案 0 :(得分:1)

InvalidOperationException应该确切地告诉您引起异常的代码行。在您的代码周围进行尝试/捕获,然后读取StackTrace属性以获取引起问题的代码行。

try
{
    output = p.StandardOutput.ReadToEnd();
    code = p.ExitCode;
    p.WaitForExit();
    string[] result = this.output.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

    // If you are updating UI controls, make sure you are doing it only on the UI thread.  The 'Dispatcher' will make sure the code inside is run on the UI thread.
    this.Dispatcher.Invoke(() =>
    {
        this.outputList.Add(Convert.ToDouble(result[0]));
        this.MyTextBox.Text = result[0];
    });
}
catch (InvalidOperationException ex)
{
    // You don't need this line, you can just put a breakpoint here to debug the problem by reading the properties of the 'ex' variable.
    this.MyTextBox.Text = $"{ex.Message}\n{ex.StackTrace}";
}