为什么我的 ReSharper 修改后的 form.Show() 代码不起作用?

发布于 2024-11-27 01:58:30 字数 639 浏览 1 评论 0原文



我正在将 ReSharper 与 C#/VS2010 结合使用。我正在运行一个单独的线程,需要打开一个表单以在主 UI 线程上向用户显示一些信息。我的代码是这样的(效果很好):

this.Invoke(new MethodInvoker(delegate()
{
    DisplayDownload form2 = new DisplayDownload();

    form2.TopMost = true;
    form2.Show();
}));



ReSharper 想将其更改为这个,我允许它(但以下代码根本不显示表单):

this.Invoke(new MethodInvoker(delegate()
{
   using (var form2 = new DisplayDownload {TopMost = true})
      {
         form2.Show();
      }
}));



使用 ReSharper 修改后的代码,表单不再显示。如果我回到旧代码,它工作得很好。

为什么ReSharper改了之后就不行了?有人可以向我解释一下是什么导致修改后的代码不再显示表单吗?

I am using ReSharper with C#/VS2010. I am running a seperate thread that needs to open a form to display some information to the user on the main UI thread. My code was this (which works great):

this.Invoke(new MethodInvoker(delegate()
{
    DisplayDownload form2 = new DisplayDownload();

    form2.TopMost = true;
    form2.Show();
}));

ReSharper wanted to change it to this and I let it (but the following code does not display the form at all):

this.Invoke(new MethodInvoker(delegate()
{
   using (var form2 = new DisplayDownload {TopMost = true})
      {
         form2.Show();
      }
}));

The form never displays anymore with the ReSharper modified code. It works fine if I go back to the old code.

Why does it not work after ReSharper changed it? Can someone please explain it to me what is causing the modified code to not display the form anymore?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2024-12-04 01:58:30

我相信这是由于 USING 关键字造成的。考虑到 Show() 不是阻塞调用,之后程序离开调用形式的 using 语句和 Dispose()。

问候。

I believe its due the USING keyword. Considering that Show() is not blocking call, after that the program leaves using statement and Dispose() of the form called.

Regards.

一江春梦 2024-12-04 01:58:30

Resharper 更改后它不起作用,因为您在显示表单后立即处理表单,因此您没有进行更改以使其显示。

然而,Resharper 希望将其更改为这种方式,因为您正在方法内创建 IDisposable 对象“DisplayDownload 表单”,并且在使用完它之后“Resharper 不知道该 Show() 方法将保持表单显示..”,您不会处置它,但是您应该在类级别声明您的 DisplayDownload 并仅在您的方法。喜欢:

DisplayDownload form2 = new DisplayDownload();

private void YourSubMethod()
{
    this.Invoke(new MethodInvoker(delegate()
    {
        form2.Show();
    }));
}

It does not work after Resharper change it because you are disposing the form just after you show it, so you did not have the change to make it shown.

However Resharper wants to change it to that way because you are creating IDisposable object "the DisplayDownload form" inside a method, and after you finish using it "Resharper doesn't know that Show() method will keep the form showing..", you does not dispose it, however you should declare your DisplayDownload at a class level and only show it on your method. like:

DisplayDownload form2 = new DisplayDownload();

private void YourSubMethod()
{
    this.Invoke(new MethodInvoker(delegate()
    {
        form2.Show();
    }));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文