非模态“状态”形式
在可能需要几秒钟才能完成的 C# 代码部分的开头,我想显示一个非模态表单,其标签只是说“请稍候...”
WaitForm myWaitForm = null;
try
{
// if conditions suggest process will take awhile
myWaitForm = new WaitForm();
myWaitForm.Show();
// do stuff
}
finally
{
if (myWaitForm != null)
{
myWaitForm.Hide();
myWaitForm.Dispose();
myWaitForm = null;
}
}
问题:WaitForm 不完全在其余代码占用线程之前显示。所以我只看到表格的框架。在 Delphi(我以前的工作场所)中,我会在 Show() 之后调用 Application.ProcessMessages C# 中是否有等效的函数?在这种情况下我可以使用预设的“状态”表格吗?有更好的方法来解决这个问题吗?
提前致谢。 大卫·詹宁斯
At the beginning of a section of C# code that could take several seconds to complete, I'd like to display a non modal form with a label that just says, "Please wait..."
WaitForm myWaitForm = null;
try
{
// if conditions suggest process will take awhile
myWaitForm = new WaitForm();
myWaitForm.Show();
// do stuff
}
finally
{
if (myWaitForm != null)
{
myWaitForm.Hide();
myWaitForm.Dispose();
myWaitForm = null;
}
}
The problem: the WaitForm doesn't completely display before the rest of the code ties up the thread. So I only see the frame of the form. In Delphi (my old stomping ground) I would call Application.ProcessMessages after the Show() Is there an equivalent in C#? Is there a canned "status" form that I can use in situations like this? Is there a better way to approach this?
Thanks in advance.
David Jennings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在不同的线程中运行 do stuff 部分。
然后所有
myWaitForm.Show()
在此处查看
BackgroundWorker
类Yo need to run the do stuff part in a different thread.
and then all
myWaitForm.Show()
Take a look at
BackgroundWorker
class here我同意“其他线程”的建议,但出于简单和简短的目的,Application.DoEvents() 就可以了。
i agree on the "other thread" suggestion, but for simple and short purposes Application.DoEvents() will do.
您最好将“do stuff”代码移至其他线程。
并使用 Application.DoEvents() 处理表单消息
You'd better move your "do stuff" code to other thread.
and use Application.DoEvents() to proces the form messages
您要查找的术语是“启动画面”。
以下是一些相关评论。 https://stackoverflow.com/search?q=splash+screen+c%23
The term you're looking for is "splash screen".
Here are a few related comments. https://stackoverflow.com/search?q=splash+screen+c%23