如何显示从另一个线程形式 [英] How to display a form from another thread

查看:128
本文介绍了如何显示从另一个线程形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在我的C#的WinForms应用程序一个单独的线程代码(我的表单的名称是MainForm的):

I am running this code in a seperate thread on my C# Winforms app (the name of my form is MainForm):

DisplayDownload form2 = new DisplayDownload();

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



但是,当线程启动时,形式永远不会打开。如果我将代码移植到我的应用程序的主线程,它会打开罚款,但如果我启动它,因为它是自己的线程,形式永远不会打开。

But when the thread is launched, the form never opens. If I move the code onto the main thread of my app, it opens fine, but if I launch it as it's own thread, the form never opens.

我试着使用从这个职位的接受的答案:调用从另一个线程Windows窗体(。网)但我得到这个错误:

I tried using the accepted answer from this post: Calling a windows form from another thread (.Net) but I get this error:

无法转换匿名方法键入'System.Delegate',因为它不是一个委托类型

Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type

下面是我想利用代码:

MainForm.Invoke(delegate {
         DisplayDownload form2 = new DisplayDownload();


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



有人能告诉我什么,我做错了,以及如何得到它的工作?

Can someone please tell me what I am doing wrong and how to get it to work?

推荐答案

添加新MethodInvoker(委托(),所以:

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


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

这篇关于如何显示从另一个线程形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆