【问题标题】:Call to Application.Run(New myForm()) not working when called outside Sub Main在 Sub Main 外部调用时调用 Application.Run(New myForm()) 不起作用
【发布时间】:2012-05-12 02:21:53
【问题描述】:

我在 Symbol MC50 上使用 Compact Framework 3.5。

在我的 Sub Main 中,它首先检查数据库是否存在。如果是,它会使用以下代码显示登录屏幕:

Dim login As frmLogin = New frmLogin()
    If login.ShowDialog() = DialogResult.OK Then            
        Application.Run(New frmMain())
    End If

这一切正常,当我关闭 frmMain 时,它会按预期退出应用程序。

但是,如果 Sub Main 中的数据库检查失败,我会调用另一个表单的 ShowDialog() 方法,该方法用于从实时服务器创建和填充数据库。下面是调用这个表单的代码:

If Not File.Exists(SETTINGS_LOCALDB) Then
        databaseExists = False
        MessageBox.Show("Local DB does not exist. The database must be created before using the application.")
        Dim update As frmUpdateData = New frmUpdateData()
        update.ShowDialog()
Else
    .....
End If

我遇到的第一个问题是,当 frmUpdateData 关闭时,Sub Main 中的其余代码没有执行,因此 Application.Run 从未被命中。

所以在 frmUpdateData 上关闭按钮的单击事件中,我添加了以下代码:

If SystemUserSecurityId() = Nothing Then
        Dim login As frmLogin = New frmLogin()
        If login.ShowDialog() = DialogResult.OK Then
            DebugTrace("Init - login complete, starting application.")
            Application.Run(New frmMain())
        End If
    End If
    Me.Hide()

所有这些代码都被命中并且 frmMain 确实加载了。但是,当我单击右上角的关闭按钮时,没有任何反应,也没有发生任何事件。就好像 Windows 事件没有发生一样。

我做错了什么?

【问题讨论】:

  • 我不确定这是否有帮助,但我会尝试使用 Application.Run 来显示您的 frmLogin 和 frmUpdateData 表单,而不是调用 ShowDialog。当您不在应用程序消息循环中时,我不知道 .NET 对您显示对话框和消息框有多友好。

标签: vb.net compact-framework


【解决方案1】:

我看不到你的整个应用程序是如何组合在一起的,但我会通过删除“else”子句来提出这个小改动:

If Not File.Exists(SETTINGS_LOCALDB) Then
  databaseExists = False
  MessageBox.Show("Local DB does not exist. The database must be created before using the application.")
  Dim update As frmUpdateData = New frmUpdateData()
  update.ShowDialog() ' Is this what populates your database???
' Else (removed the else clause)
End If
If File.Exists(SETTINGS_LOCALDB) Then ' now it is OK to run, correct?
  If SystemUserSecurityId() = Nothing Then
    Dim login As frmLogin = New frmLogin()
    If login.ShowDialog() = DialogResult.OK Then
      DebugTrace("Init - login complete, starting application.")
      Application.Run(New frmMain())
    End If
  End If
End If

【讨论】:

    【解决方案2】:

    您的主窗体不会关闭的原因是您的应用程序“卡”在对话框的关闭事件的调用堆栈中,因为这是您为主窗体启动 Windows 消息循环的地方。

    我建议您对代码进行一些重组。

    不要在“主”子中进行有效性检查,而是加载主表单:

    Application.Run(New frmMain())
    

    在主窗体上放置一个计时器并设置一个非常快的间隔(比如 10 毫秒)。在主窗体的加载事件中启用它。像这样实现 Tick 事件处理程序(请注意,我的 VB 语法可能并不完美,我在这里进行改进):

       Sub TmrOneShot_Tick(ByVal sender as Object, ByVal e as System.EventArgs)
        'prevent timer from firing again.
        tmrOneShot.Enabled = False;
        Dim bContinue as Boolean = False;
    
        If Not File.Exists(SETTINGS_LOCALDB) Then
          databaseExists = False
          MessageBox.Show("Local DB does not exist. The database must be created before using the application.")
          Dim update As frmUpdateData = New frmUpdateData()
          update.ShowDialog() ' Is this what populates your database???
    
          'analyze result of update form to determine if you should continue...
          bContinue = WasUpdateDataOperationSuccessful();
    
        End If
    
       If bContinue Then
          If SystemUserSecurityId() = Nothing Then
            Dim login As frmLogin = New frmLogin()
            bContinue = login.ShowDialog() = DialogResult.OK
            if bContinue Then
              DebugTrace("Init - login complete, starting application.")              
            End If
          End If
        End If
    
      If Not bContinue Then
         'can't continue, terminate app
         Application.Exit()
      End if
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2018-08-29
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多