We help IT Professionals succeed at work.

Console Application "Sleep"

824 Views
Last Modified: 2013-11-07
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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

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

Author

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

Author

Commented:
and of course once that process is completed i want it to sit idle for 1 miinute and then begin over again.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

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

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you want to log, look into log4net.

Bob
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.