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.
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. Connection String)
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Try
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedur e
cmd.CommandText = "dbo.Sales_Assistant_Messa ge_Sender"
dr = cmd.ExecuteReader
While dr.Read()
If Not dr.Item("Email").Equals(DB Null.Value ) Then SendMail(dr.Item("email"), dr.Item("MessageText"))
If Not dr.Item("TextMsgEmail").Eq uals(DBNul l.Value) Then SendMail(dr.Item("TextMsgE mail"), 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
Sub Main()
Dim conn As New SqlConnection(My.Settings.
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Try
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedur
cmd.CommandText = "dbo.Sales_Assistant_Messa
dr = cmd.ExecuteReader
While dr.Read()
If Not dr.Item("Email").Equals(DB
If Not dr.Item("TextMsgEmail").Eq
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
With 2005, you could have Using blocks, which would simplify your code a little:
Using conn As New SqlConnection(My.Settings. Connection String)
conn.Open()
Using cmd As New SqlCommand("dbo.Sales_Assi stant_Mess age_Sender ", conn)
cmd.CommandType = CommandType.StoredProcedur e
Using dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If dr("Email").ToString().Len gth > 0 Then
SendMail(dr("email").ToStr ing(), dr("MessageText").ToString ())
End If
If dr("TextMsgEmail").ToStrin g().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
Using conn As New SqlConnection(My.Settings.
conn.Open()
Using cmd As New SqlCommand("dbo.Sales_Assi
cmd.CommandType = CommandType.StoredProcedur
Using dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If dr("Email").ToString().Len
SendMail(dr("email").ToStr
End If
If dr("TextMsgEmail").ToStrin
SendMail(dr("TextMsgEmail"
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
ASKER
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
Bob
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