为什么我的 ReSharper 修改后的 form.Show() 代码不起作用?
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这是由于 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.
Resharper 更改后它不起作用,因为您在显示表单后立即处理表单,因此您没有进行更改以使其显示。
然而,Resharper 希望将其更改为这种方式,因为您正在方法内创建
IDisposable
对象“DisplayDownload
表单”,并且在使用完它之后“Resharper 不知道该Show()
方法将保持表单显示..”,您不会处置它,但是您应该在类级别声明您的DisplayDownload
并仅在您的方法。喜欢: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 "theDisplayDownload
form" inside a method, and after you finish using it "Resharper doesn't know thatShow()
method will keep the form showing..", you does not dispose it, however you should declare yourDisplayDownload
at a class level and only show it on your method. like: