在控制台应用程序中的特定点(在执行过程中)显示表单 [英] Display a form in a console application at a particular point(in the middle of execution)

查看:24
本文介绍了在控制台应用程序中的特定点(在执行过程中)显示表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制台应用程序中间显示一个表单以显示一些状态信息并在发生特定事件时自动关闭表单.以下代码部分是否足以达到此目的?

I would like to display a form in the middle of a console application to display some status information and automatically close the form when a particular event occurs. Will the following code portion will be enough for this purpose?

显示表单

ModuleInitializerForm moduleInitializerDlg = new ModuleInitializerForm()
{
  Parent = parent,
  TopMost = true,
  TopLevel = true,
  Text = Common.MESSAGE_INFO_TITLE,
  ControlBox = false,
  FormBorderStyle = FormBorderStyle.FixedDialog,
  KeyPreview = false,
};

moduleInitializerDlg.Initialize();
moduleInitializerDlg.ShowDialog(); 

关闭表单

public void OnModuleInitializationCompleted(object sender, EventArgs e)
  {
    if (this.InvokeRequired)
    {
      this.BeginInvoke(new ECEventsHandler(OnModuleInitializationCompleted), sender, e);
    }
    else
    {
      this.Close();
    }
  }

推荐答案

由于您使用 ShowDialog(),执行此操作的线程将被阻塞,直到用户(或您)关闭表单.

Since you are using ShowDialog(), the thread where this is done will be blocked until the user (or you) close the form.

请注意,如果您改用 Show(),线程将继续.但是,如果没有人在该线程上发送 Windows 消息,则该表单将是死的",即它不会响应任何内容.如果您从一个普通的控制台应用程序项目开始,就会出现这种情况.如果您使用 ShowDialog(),它会创建一个本地消息循环,以便窗口做出响应.

Note that if you used Show() instead, the thread will continue. However, if you have no-one pumping windows messages on that thread, the form will be "dead", i.e. it won't respond to anything. If you start with a normal console app project, then this will be the case. If you use ShowDialog(), it creates a local message loop so the window responds.

因此,您需要有一个单独的线程用于显示表单的 Windows 窗体 UI.我会使用 Application.Run(moduleInitializerDlg); 而不是 ShowDialog(),因为据我所知,它可以更好地设置/拆除消息泵架构.顺便说一下,该调用会以与 ShowDialog() 相同的方式阻塞线程.

So you will need have a separate thread for the Windows Forms UI where you show the form. I would use Application.Run(moduleInitializerDlg); instead of ShowDialog(), since it better sets up/tears down the message pumping architecture as far as I can tell. That call will block the thread in the same way as ShowDialog() by the way.

这篇关于在控制台应用程序中的特定点(在执行过程中)显示表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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