首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InvalidOperationException病因不明?

InvalidOperationException病因不明?
EN

Stack Overflow用户
提问于 2020-06-22 15:48:01
回答 1查看 35关注 0票数 1

我有一个字符串数组,用于将输出与另一个进程分开。然后,我希望在我的WPF接口中的textbox中显示该数组的第一个元素。但是,当我尝试这样做的时候,我收到了这个异常,有什么想法来解决它吗?

代码语言:javascript
复制
        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];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-22 16:11:32

InvalidOperationException应该确切地告诉您是哪一行代码导致了异常。在代码周围放置一个try/catch,然后读取StackTrace属性以获得导致问题的代码行。

代码语言:javascript
复制
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}";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62518381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档