非模态“状态”形式

发布于 2024-09-03 18:36:50 字数 543 浏览 5 评论 0原文

在可能需要几秒钟才能完成的 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 技术交流群。

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

发布评论

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

评论(4

梦开始←不甜 2024-09-10 18:36:50

您需要在不同的线程中运行 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

烟织青萝梦 2024-09-10 18:36:50

我同意“其他线程”的建议,但出于简单和简短的目的,Application.DoEvents() 就可以了。

i agree on the "other thread" suggestion, but for simple and short purposes Application.DoEvents() will do.

浅暮の光 2024-09-10 18:36:50

您最好将“do stuff”代码移至其他线程。
并使用 Application.DoEvents() 处理表单消息

You'd better move your "do stuff" code to other thread.
and use Application.DoEvents() to proces the form messages

梦里兽 2024-09-10 18:36:50

您要查找的术语是“启动画面”。

以下是一些相关评论。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文