Link to home
Start Free TrialLog in
Avatar of bergertime
bergertime

asked on

VB.net Close program with timer

I want to do a windows app that closes after 30 seconds.  So I put a timer control on the form, and add this code in the form load:

 Timer1.Interval = 30000 'for 5 second and 10000 for 10 seconds
        Timer1.Start()

then I add this:

Private Sub Timer1_tick()
        Me.Close()
    End Sub


Thanks
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Please copy and paste the actual code.

And what is your question?
Hi bergertime;

It looks like you forgot to tell the application that the Timer1_Tick handles the tick for the object Timer1.Tick.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Interval = 30000
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Me.Close()
    End Sub
End Class

Open in new window

Avatar of bergertime
bergertime

ASKER

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim parameter1 As String = 1
        Dim parameter2 As String = 1
        Dim parameter3 As String = 1
        
        For Each argument As String In My.Application.CommandLineArgs
            Try
                parameter1 = My.Application.CommandLineArgs(0).ToString
            Catch
            End Try
            Try
                parameter2 = My.Application.CommandLineArgs(1).ToString
            Catch
            End Try
            Try
                parameter3 = My.Application.CommandLineArgs(2).ToString
            Catch
            End Try

            parameter1 = parameter1.TrimStart("0"c)
            parameter2 = parameter2.TrimStart("0"c)
            parameter3 = parameter3.TrimStart("0"c)
            TextBox1.Text = parameter1
            TextBox2.Text = parameter2
            TextBox7.Text = parameter3
        Next
        Timer1.Interval = 30000 'for 5 second and 10000 for 10 seconds
        Timer1.Start()

        If TextBox1.Text = "" Then
            '  Me.Close()
        Else
            Try
                ' WriteLog()
                'LoadUserDS()
                'SendMessage()
                'Me.Close()
            Catch
            End Try
        End If

        ' Me.Close()

    End Sub

    Private Sub Timer1_tick()
        Me.Close()
    End Sub

End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol, sorry.  Thanks so much!!!
Not a problem. Glad to help.

On your form load event. You have Try/Catch blocks with out any Catch statements to actually Catch the error. This is a bad programming practice because when you get an exception thrown it will be swallowed up and you will not know where the error is coming from, so if you use a Try/Catch block catch the exception. Also Try/Catch blocks are expensive and should NOT be used as you did in the code, re-think this.
Hey thanks for the tip.  I do it that way because if not, then my program errors out and gets stuck on null values.  I don't really care what the error is as I can always refer to my log, but it was the only way I could come up with to pass nulls.  Is there a better way.  I can open a new thread so you can get the points but I get the impression you care more about helping than points.  Thanks again!!
Are you talking about the parameters being passed into the application not all being passed in?
Correct, if one of those happens to be null, my app would get stuck.  That was during the development phase though.  So I guess I can just remove the lines as none of them should ever be blank.  But how would I handle that otherwise?  Maybe a if then

if My.Application.CommandLineArgs(0).ToString = "" then
else
parameter = My.Application.CommandLineArgs(0).ToString

Maybe?  Is that better than a try catch block?
Hi bergertime;

Here is a code snippet of two different situations. First situation is having the same number of CommandLine arguments and exiting the program if that number is not reached. Second situation is using a key value pair on the command line for each argument so that you can differentiate them within your code. Please note that Error checking was not implemented in the code snippet so you need to implement any in your code.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    '' A List of String's initialized max number of String's init to ""
    Dim parameters As New List(Of String) From {"", "", ""}
    Dim argCount As Integer = My.Application.CommandLineArgs().Count()

    '' If you will always have 3 CommandLine arguments then you can test 
    '' to see if you have 3 or not. If not display a message and exit
    If Not argCount = 3 Then
        MessageBox.Show("Missing CommanLine Parameters, Check and restart")
        Me.Close()
    End If

    '' If you can have any number of CommandLine arguments then you can do something
    '' like this, max 3 args in this case. These are the CommandLine args note that 
    '' I used a key value pare, key=value, so that they can be entered in any order 
    '' and not case sensitive.
    '' arg3=third ArG2=Second arg1=First
    If argCount > 0 Then
        For Each arg As String In My.Application.CommandLineArgs()
            Select Case arg.Substring(0, 4).ToUpper()
                Case "ARG1"
                    parameters(0) = arg.Substring(5)
                Case "ARG2"
                    parameters(1) = arg.Substring(5)
                Case "ARG3"
                    parameters(2) = arg.Substring(5)
            End Select
        Next
    End If

    Console.WriteLine()

End Sub

Open in new window