Link to home
Start Free TrialLog in
Avatar of rickydoyle
rickydoyle

asked on

Console Application "Sleep"

I have built a console application and I would like to keep it running at all times.  So once the application has completed I would like to have it sit/sleep for 1 minute and then re-run again.  Also it would be great if somehow I could keep the connection to the db opened so that I don't have to open a new connection every minute.  Any input/thoughts on this would be greatly appreciated.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) What database type are you using?

2) With connection pooling, opening a connection is trivial.

3) You can use a While True loop, with a Thread.Sleep(60000) to sleep for a minute.

Bob
Avatar of rickydoyle
rickydoyle

ASKER

thanks for the quick response bob.  I am using sql server 2005.  Here is a basic version of what I am doing.  

Sub Main()
        Dim conn As New SqlConnection(My.Settings.ConnectionString)
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader

        Try
            conn.Open()
            cmd.Connection = conn
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "dbo.Sales_Assistant_Message_Sender"

            dr = cmd.ExecuteReader
            While dr.Read()
                If Not dr.Item("Email").Equals(DBNull.Value) Then SendMail(dr.Item("email"), dr.Item("MessageText"))
                If Not dr.Item("TextMsgEmail").Equals(DBNull.Value) Then SendMail(dr.Item("TextMsgEmail"), dr.Item("MessageText"))
            End While
            dr.Close()
            cmd.Dispose()
        Catch ex As Exception
            Throw New Exception(ex.ToString)
        Finally
            If conn.State = ConnectionState.Open Then conn.Close()
            conn.Dispose()
        End Try

    End Sub
and of course once that process is completed i want it to sit idle for 1 miinute and then begin over again.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
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
With 2005, you could have Using blocks, which would simplify your code a little:

    Using conn As New SqlConnection(My.Settings.ConnectionString)
      conn.Open()
      Using cmd As New SqlCommand("dbo.Sales_Assistant_Message_Sender", conn)
        cmd.CommandType = CommandType.StoredProcedure
        Using dr As SqlDataReader = cmd.ExecuteReader()
          While dr.Read()
            If dr("Email").ToString().Length > 0 Then
              SendMail(dr("email").ToString(), dr("MessageText").ToString())
            End If
            If dr("TextMsgEmail").ToString().Length > 0 Then
              SendMail(dr("TextMsgEmail").ToString(), dr("MessageText").ToString())
            End While
        End Using
      End Using
    End Using

Also, if you were using the Try/Catch block to re-throw the exception, you can just leave it out with the same effect.

Bob
Ok thanks for the second answer as well bob.  I am planning on putting some logging into the application as well for failed transactions.  Thanks for all your help.
If you want to log, look into log4net.

Bob