【问题标题】:Close form after 10 seconds10 秒后关闭表格
【发布时间】:2011-05-29 04:59:36
【问题描述】:

我有一个可以打开其他表单的 Windows 表单应用程序,但只会显示表单几秒钟(用户可配置)。我通常会做类似 threading.thread.sleep(n) 之类的事情,但是当这样做时,表单控件不会只加载白色背景显示,而且我也一直在读到这不是我作为用户所追求的最佳实践在线程唤醒之前不会对输入进行操作。

我遇到过使用 System.Timers.Timer(n) 的人,但我很难让它为我工作,表单只会立即打开和关闭(您只能在表单打开时看到闪烁然后关闭)。

我使用的代码是:

Private Shared tmr As New System.Timers.Timer    
aForm.Show()
tmr = New System.Timers.Timer(aSleep * 60 * 60)
tmr.Enabled = True

aForm.Close()

这都包含在传递表单和定义的运行时间的 Private sub 中。

我的目的是让主应用程序从任务栏运行,然后调用其中一个将在定义的时间段内显示的表单,关闭该表单,然后调用另一个表单。

有没有人能够为我指出正确的方向,为什么表单打开然后关闭而没有看到定义的运行时间(我一直在测试 10 秒),或者有没有更好的方法来做我正在寻找的事情?

非常感谢您的帮助。

马特

【问题讨论】:

    标签: vb.net .net-3.0


    【解决方案1】:

    文档说有一个 Elapsed 事件处理程序在时间过去时被调用。您将在处理程序中关闭表单:

    http://msdn.microsoft.com/en-us/library/system.timers.timer%28VS.85%29.aspx

    我刚刚写了一个小例子来说明你需要做什么:

    http://www.antiyes.com/close-form-after-10-seconds

    下面是相关代码,完整的解决方案可以从文章中下载。

    表格 1 代码

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frm2 As New Form2()
            frm2.ShowDialog()
        End Sub
    
    End Class
    

    Form 2 代码

    Imports System.Timers
    
    Public Class Form2
    
        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            MyBase.OnLoad(e)
            Dim tmr As New System.Timers.Timer()
            tmr.Interval = 5000
            tmr.Enabled = True
            tmr.Start()
            AddHandler tmr.Elapsed, AddressOf OnTimedEvent
        End Sub
    
        Private Delegate Sub CloseFormCallback()
    
        Private Sub CloseForm()
            If InvokeRequired Then
                Dim d As New CloseFormCallback(AddressOf CloseForm)
                Invoke(d, Nothing)
            Else
                Close()
            End If
        End Sub
    
        Private Sub OnTimedEvent(ByVal sender As Object, ByVal e As ElapsedEventArgs)
            CloseForm()
        End Sub
    
    End Class
    

    当然,要使此代码正常工作,您需要使用按钮设置表单。

    【讨论】:

    • 我看不到如何使用它,我想在计时器结束后关闭表单,但是我看不到将表单名称传递给 OnTimedEvent 的方法Sub,并从 sub 调用 Me.Close() 会导致我尝试从另一个线程访问线程的错误。
    • 我已经编辑了我的答案并给出了一个更完整的例子。前往antiyes.com/close-form-after-10-seconds下载示例vs2008解决方案。
    【解决方案2】:

    您的代码设置了一个计时器,然后立即关闭表单。关闭必须在定时器事件触发时完成。

    【讨论】:

      【解决方案3】:

      我想我可以稍微扩展一下乔纳森的回答。

      在您希望在给定时间内显示的表单上,添加一个计时器(在此示例中,计时器名为 Timer1...计时器可以在工具箱中找到,只需将其拖到表单上)

      要让表单在显示给定时间后关闭,在表单的 onload 方法中启动计时器:

      Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          'Do initialization stuff for your form...
          'Start your timer last.
          Timer1.Start()
      End Sub
      

      这将启动您的计时器。当预设时间过去时,tick 事件将触发。在这种情况下,放置您的表单关闭代码:

      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
          'Close the form after 1 tick.
          Me.Close()
      End Sub
      

      要更改在计时器滴答之前应该经过多少时间,请更改计时器间隔属性。

      'Changing the time from outside the Form1 class...
      Form2.Timer1.Interval = 2000 '2 seconds, the interval is in milliseconds.
      

      完整代码,form1有一个按钮,设置定时器间隔,然后打开form2。

      Public Class Form1
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              Form2.Timer1.Interval = 2000
              Form2.Show()
          End Sub
      End Class
      
      
      Public Class Form2
          Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              'Do initialization stuff for your form...
              'Start your timer last.
              Timer1.Start()
          End Sub
      
          Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
              Me.Close()
          End Sub
      End Class
      

      我希望这会有所帮助,如果我能以更清晰的方式重述任何内容,请告诉我 :-)

      【讨论】:

        【解决方案4】:

        只需在表单上粘贴一个时间组件并启用它。在 tick 事件处理程序中,确定表单打开后的时间以及是否已过预期时间,关闭表单。

        通过允许 Form_Load 句柄在您等待 Tick 事件时返回,表单可以进行绘制并执行其他正常操作。

        虽然您可以按照自己的方式从代码中创建时间,但我不确定您为什么会这样做。而且您肯定需要为 Tick 事件设置一个处理程序才能发挥作用。

        【讨论】:

        • 您能否提供一个示例或链接,说明如何确定表单打开后的时间(以毫秒为单位)?我已经尝试在 WWW 上查找,但到目前为止还没有取得太大的成功。
        • 我现在不在我的开发机器上。但只需在表单的 Load 事件处理程序中创建一个 DateTime 类型的表单变量并将其设置为 DateTime.Now。然后,在 Tick 事件处理程序中,只需计算它已经过了多长时间。你会很难精确到毫秒,但你应该能够足够接近。
        【解决方案5】:

        如果 frmTemperatureStatus 关闭,则在设定时间(在本例中)打开表单的真正简单方法将跳过计时器。我将 frmTemperatureStatus 作为普通表单打开,而不是作为对话框打开,否则代码会跳转到此表单并且在表单关闭之前不会返回。 DoEvents 使 frmTemperatureStatus 保持响应(注意:如果您逐行测试代码,frmTemperatureStatus 将保持失去焦点,因为焦点会不断返回到 Visual Studio)。

                    timeIncubation_End_Time = Now.AddMinutes(1)
                    Me.Enabled = False
                    frmTemperature_Status.Show()
        
                    Do While frmTemperature_Status.Visible And timeIncubation_End_Time > Now
                        Application.DoEvents()
                        Threading.Thread.Sleep(100)
                    Loop
                    frmTemperature_Status.Close() ' This line doesn't cause an error if the form is already closed
                    Me.Enabled = True
                    MsgBox("End of dialog test")
        

        【讨论】:

          【解决方案6】:
          Public Class Form1
              Dim second As Integer
          
              Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  Timer1.Interval = 1000
                  Timer1.Start() 'Timer starts functioning
              End Sub
          
              Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
                  Label1.Text = DateTime.Now.ToString
          
                  second = second + 1
                  If second >= 10 Then
                      Timer1.Stop() 'Timer stops functioning
                      Me.Close()
                      MsgBox("Timer Stopped....")
                  End If
          
              End Sub
          
          End Class
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-20
            • 1970-01-01
            相关资源
            最近更新 更多