在一个线程中最顶层的形式? [英] TopMost form in a thread?
问题描述
我使用下面的代码打开一个新的线程形式:
I am using the following code to open a form in a new thread:
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(ThreadProc);
thread.Start();
}
public void ThreadProc()
{
Form form = new Form();
form.TopMost = true;
form.ShowDialog();
}
不过,新创建的窗体不是最顶层,即使我将它设置为true
But the newly created form isn't TopMost even though I set it to true.
我怎样才能使一个形式,一个线程最顶层的?
How can I make a form in a thread TopMost ?
推荐答案
通常你不需要另一个线程,你打开窗体为模式或无模态模式通常,如果表单需要做一个沉重的过程,那么你做一个线程中的过程。结果
< BR>
具体到你的问题一种选择是从Application.Run运行形式描述的这里。
Usually you don't need another thread, you open the form as usual in modal or non modal mode, if the form needs to do a heavy process then you do the process inside a thread.
Specific to your question one option is to run the form from an Application.Run as described here.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(ThreadProc);
thread.Start();
}
public void ThreadProc()
{
using (Form1 _form = new Form1())
{
_form.TopMost = true;
Application.Run(_form);
}
}
}
这将推出一个新的线程有自己的消息泵,并保持它作为一个最顶层的形式。
That will launch a new thread with its own message pump and will keep it as a TopMost form.
这篇关于在一个线程中最顶层的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!