Is There a Way to Remove the Delay between an Interspersed Login form and the Main form?

邮差的信 提交于 2019-12-13 04:47:16

问题


Following the suggestion here to create my login form in the project's Main method like so:

[MTAThread]
static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += Unhandled;

    frmLogin loginForm = new frmLogin();
    if (loginForm.ShowDialog() != DialogResult.OK)
    {
        // If they hit "Close" just use the default values for now (for testing)
        HHSConsts.userName = "duckbilled";
        HHSConsts.pwd = "platypus";
        HHSConsts.currentSiteNum = "Packers20Seahawks19";
    }
    else
    {
        HHSConsts.userName = loginForm.UserName;
        HHSConsts.pwd = loginForm.Password;
        HHSConsts.currentSiteNum = loginForm.SiteNumber;
    }
    loginForm.Dispose();

    Application.Run(new frmMain());
}

The login form has two buttons, "OK" and "Close":

private void buttonOK_Click(object sender, EventArgs e)
{
    HHSConsts.userName = textBoxUsername.Text.Trim();
    HHSConsts.pwd = textBoxPwd.Text.Trim();
    HHSConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedItem.ToString();
    // TODO: Prevent shutdown if "OK" is selected and there are any missing or bogus values?
    this.Close();
}

private void buttonClose_Click(object sender, EventArgs e)
{
    this.Close();
}

This works pretty fair, but there is a definite delay between the login form closing and the main form displaying. Is there a way to close this gap so that the interval is not so noticeable?

UPDATE

Replacing this:

Application.Run(new frmMain());

...with this:

Application.Run(new Form());

...in Program.cs results in the following:

Clicking the OK button on the login form: app closes (main form never displays)

Clicking the Close button on the login form: all the controls on the login form disappear, but the form stays up...?!?


回答1:


Try pre-loading the main form with as much as you can:

[MTAThread]
static void Main()
{
  var mainForm = new frmMain();

  using(loginForm = new frmLogin())
  {
    if (loginForm.ShowDialog() != DialogResult.OK)
    {
      // If they hit "Close" just use the default values for now (for testing)
      HHSConsts.userName = "duckbilled";
      HHSConsts.pwd = "platypus";
      HHSConsts.currentSiteNum = "Packers20Seahawks19";
    }
    else
    {
      HHSConsts.userName = loginForm.UserName;
      HHSConsts.pwd = loginForm.Password;
      HHSConsts.currentSiteNum = loginForm.SiteNumber;
    }
  }

  mainForm.NotifyTheFormInstanceTheCredentialsHaveChangedIfItIsNotEventDrivenAlready();

  Application.Run(mainForm);
}


来源:https://stackoverflow.com/questions/27909793/is-there-a-way-to-remove-the-delay-between-an-interspersed-login-form-and-the-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!