Pregunta

Tengo una aplicación de Windows Forms que abrirá otras formas, pero sólo mostrará las formas de algunos segundos (configurable por el usuario). Me gustaría hacer algo como normalmente threading.thread.sleep (n), sin embargo al hacer esto las formas controles no cargar sólo el fondo blanco espectáculos, y también he estado leyendo que esta no es la mejor práctica por lo que soy después de que el usuario de entrada no se acciona hasta que el hilo se despierta.

Me he encontrado con personas que utilizan System.Timers.Timer (n), pero estoy luchando para conseguir que esto funcione para mí, el formulario sólo se abren y se cierran de inmediato (sólo se puede ver un flash como el formulario se abre después se cierra).

El código que estoy usando es:

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

aForm.Close()

Esto es todo lo contenido dentro de un sub privada que pasa a la forma y el tiempo de funcionamiento definido.

Mi intención es tener la aplicación principal que va desde la barra de tareas, que llama a continuación, una de las formas que se mostrarán durante un periodo de tiempo definido, cerca de la forma, y ??luego llamar a otra de las formas.

Está alguno capaz de apuntar en la dirección correcta para qué se abre el formulario a continuación, se cierra sin ver a través del tiempo de ejecución definido (he estado probando con 10 segundos), o hay una mejor manera de hacer lo que estoy buscando?

Su ayuda es muy apreciada.

Matt

¿Fue útil?

Solución

los documentos dicen que hay un controlador de eventos transcurrido que se llama cuando las transcurre el tiempo. Se podría cerrar el formulario en el controlador:

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

Me acaba de escribir un pequeño ejemplo que muestra lo que tendrían que hacer en:

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

A continuación se muestra el código en cuestión, la solución completa se puede descargar desde el artículo.

Forma 1 código

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

código Forma 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

Por supuesto, para que este código funcione que había necesidad de instalación formas con los botones.

Otros consejos

Your code sets a timer then immediately closes the form. The closing must be done when the timer event fires.

I think I can expand on Jonathan's answer a bit.

On the form that you wish to display for a given amount of time, add a timer (in this example the timer is named Timer1...Timers can be found in the toolbax, just drag it onto the form)

To have the form close after it has been displayed for a given amount of time start the timer in the onload method of the form:

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

This will start your timer. When the preset time elapses, the tick event will fire. In this event place your form closing code:

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

To change how much time should elapse before the timer ticks, change the timer interval property.

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

Full code, form1 has a button that sets the timer interval and then opens 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

I hope this helps, let me know if I can restate anything in a more clear fashion :-)

Just stick a time component on the form and enable it. In the tick event handler, determine how long since the form opened and if the intended period has elapses, close the form.

By allowing the Form_Load handle to return while you wait for the Tick event, the form is allowed to paint and do everything else it normally would.

While you could create the time from code as you are doing, I'm not sure why you would. And you definitely need to set a handler for the Tick event for it to do any good.

A real simple way of opening a form for a set time (and in this example) will skip timer if frmTemperatureStatus is closed. I'm opening frmTemperatureStatus as a normal form not as a dialog otherwise the code jumps to this form and doesn't return until the form is closed. The DoEvents keeps frmTemperatureStatus responsive (Note: frmTemperatureStatus will keep losing focus if you test code line by line as focus keeps going back to 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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top