开发者

Display a form in a console application at a particular point(in the middle of execution)

开发者 https://www.devze.com 2023-02-28 21:05 出处:网络
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 portio

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?

Display the form

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(); 

Close the form

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


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

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.

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.


go to the properties of the form wich should open on click... go to the Layout section and put Start position to Centerscreen or if u have a parent in wich u want to dock it each time, use CenterParent...

enjoy!

0

精彩评论

暂无评论...
验证码 换一张
取 消