10秒后关闭表格

我有一个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子中。 我的目的是让主应用程序从任务栏运行,然后任务栏调用将在指定时间段内显示的表单之一,关闭表单,然后调用另一个表单。 有没有能够指出我正确的方向为什么表格打开然后关闭而不通过定义的运行时间(我已经测试了10秒),或者有更好的方式来做我正在寻找的东西? 非常感谢您的帮助。 马特     
已邀请:
文档说有一个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
表格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
当然,要使此代码工作,您需要使用按钮进行表单设置。     
您的代码设置计时器,然后立即关闭表单。必须在计时器事件触发时完成关闭。     
我想我可以稍微扩展Jonathan的答案。 在您希望显示给定时间的表单上,添加一个计时器(在此示例中,计时器名为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
这将启动您的计时器。当预设时间过去时,勾选事件将触发。在此活动中,请填写表单结束代码:
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
我希望这有帮助,如果我能以更清晰的方式重述任何内容,请告诉我:-)     
只需在表单上粘贴一个时间组件并启用它即可。在tick事件处理程序中,确定表单打开后的时间以及预期的时间段是否已过,关闭表单。 通过在等待Tick事件时允许Form_Load句柄返回,允许表单绘制并执行其通常的所有操作。 虽然你可以在你做的时候从代码中创造时间,但我不确定你为什么会这样做。你肯定需要为Tick事件设置一个处理程序,以便它做任何好事。     
如果关闭了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")
    
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
    

要回复问题请先登录注册