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
LVL 2
bergertimeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Please copy and paste the actual code.

And what is your question?
0
Fernando SotoRetiredCommented:
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

0
bergertimeAuthor Commented:
 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

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Fernando SotoRetiredCommented:
Read my last post about this, look at my version of this Sub.
Private Sub Timer1_tick()
        Me.Close()
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bergertimeAuthor Commented:
lol, sorry.  Thanks so much!!!
0
Fernando SotoRetiredCommented:
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.
0
bergertimeAuthor Commented:
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!!
0
Fernando SotoRetiredCommented:
Are you talking about the parameters being passed into the application not all being passed in?
0
bergertimeAuthor Commented:
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?
0
Fernando SotoRetiredCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.