By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

Thread does not sleep in Console application

100+
P: 375
Hello
I have written the following in console application.
My problem is I want the application running in the back.
and it should check every thirty minutes and update thedatabase

the problem is that the threading does not work
and to make the application run every thirty minutes am i suppose to create a windows service.

Please let me know
Thanks


Expand|Select|Wrap|Line Numbers
  1.  Sub Main()
  2.         Dim Th As New Thread(AddressOf CheckTimeDifference)
  3.  
  4.         Th.Start()
  5.  
  6.  
  7.     End Sub
  8.     Private Sub CheckTimeDifference()
  9.  
  10.         Try
  11.  
  12.             MyCon = New SqlConnection("server=ccccccc;uid=;pwd=;database=ee")
  13.             MyCon.Open()
  14.  
  15.             chkTime = Now
  16.             StrSql1 = "Select REGISTRATIONNO,MAX(GPS_DATETIME) FROM GPSDATA_HISTORY group by REGISTRATIONNO"
  17.             MyCmd = New SqlCommand(StrSql1, MyCon)
  18.  
  19.             Da = New SqlDataAdapter(StrSql1, MyCon)
  20.             Ds = New DataSet()
  21.             Da.Fill(Ds, "ChkTable")
  22.  
  23.             RowCount = (Ds.Tables("ChkTable").Rows.Count)
  24.             For I = 0 To RowCount - 1
  25.  
  26.                 RegNo = (Ds.Tables("chktable").Rows(I)(0))
  27.                 dataTime = (Ds.Tables("chktable").Rows(I)(1))
  28.  
  29.                 TimeDiff = DateDiff(DateInterval.Minute, dataTime, chkTime)
  30.                 If TimeDiff > 30 Then
  31.                     StrSql1 = "Update gpsdata set speed=0 ,ignition=0 where registrationno='"
  32.                     StrSql2 = RegNo & "'"
  33.                     StrSql = StrSql1 + StrSql2
  34.                     MyCmd1 = New SqlCommand(StrSql, MyCon)
  35.                     MyCmd1.ExecuteNonQuery()
  36.                     Console.WriteLine("Speed details, Ignition Details has been changed in GPS DATA")
  37.                     StrSql3 = "Update gpsdata_history set speed=0 ,ignition=0 where registrationno='"
  38.                     StrSql4 = RegNo & "' and gps_datetime='" & dataTime & "'"
  39.                     StrSql5 = StrSql3 + StrSql4
  40.                     MyCmd2 = New SqlCommand(StrSql5, MyCon)
  41.                     MyCmd2.ExecuteNonQuery()
  42.                     Console.WriteLine("Speed details, IgnitionDetails has been changed in GPSDATAHISTORY")
  43.                 End If
  44.  
  45.             Next
  46.  
  47.         Catch e As Exception
  48.  
  49.             Console.WriteLine(e.ToString)
  50.             Thread.Sleep(60000)
  51.  
  52.         End Try
  53.  
  54.     End Sub
  55. End Module
Jul 23 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 375
Hello
I have written the following in console application.
My problem is I want the application running in the back.
and it should check every thirty minutes and update thedatabase

the problem is that the threading does not work
and to make the application run every thirty minutes am i suppose to create a windows service.

Please let me know
Thanks


Expand|Select|Wrap|Line Numbers
  1.  Sub Main()
  2.         Dim Th As New Thread(AddressOf CheckTimeDifference)
  3.  
  4.         Th.Start()
  5.  
  6.  
  7.     End Sub
  8.     Private Sub CheckTimeDifference()
  9.  
  10.         Try
  11.  
  12.             MyCon = New SqlConnection("server=ccccccc;uid=;pwd=;database=ee")
  13.             MyCon.Open()
  14.  
  15.             chkTime = Now
  16.             StrSql1 = "Select REGISTRATIONNO,MAX(GPS_DATETIME) FROM GPSDATA_HISTORY group by REGISTRATIONNO"
  17.             MyCmd = New SqlCommand(StrSql1, MyCon)
  18.  
  19.             Da = New SqlDataAdapter(StrSql1, MyCon)
  20.             Ds = New DataSet()
  21.             Da.Fill(Ds, "ChkTable")
  22.  
  23.             RowCount = (Ds.Tables("ChkTable").Rows.Count)
  24.             For I = 0 To RowCount - 1
  25.  
  26.                 RegNo = (Ds.Tables("chktable").Rows(I)(0))
  27.                 dataTime = (Ds.Tables("chktable").Rows(I)(1))
  28.  
  29.                 TimeDiff = DateDiff(DateInterval.Minute, dataTime, chkTime)
  30.                 If TimeDiff > 30 Then
  31.                     StrSql1 = "Update gpsdata set speed=0 ,ignition=0 where registrationno='"
  32.                     StrSql2 = RegNo & "'"
  33.                     StrSql = StrSql1 + StrSql2
  34.                     MyCmd1 = New SqlCommand(StrSql, MyCon)
  35.                     MyCmd1.ExecuteNonQuery()
  36.                     Console.WriteLine("Speed details, Ignition Details has been changed in GPS DATA")
  37.                     StrSql3 = "Update gpsdata_history set speed=0 ,ignition=0 where registrationno='"
  38.                     StrSql4 = RegNo & "' and gps_datetime='" & dataTime & "'"
  39.                     StrSql5 = StrSql3 + StrSql4
  40.                     MyCmd2 = New SqlCommand(StrSql5, MyCon)
  41.                     MyCmd2.ExecuteNonQuery()
  42.                     Console.WriteLine("Speed details, IgnitionDetails has been changed in GPSDATAHISTORY")
  43.                 End If
  44.  
  45.             Next
  46.  
  47.         Catch e As Exception
  48.  
  49.             Console.WriteLine(e.ToString)
  50.             Thread.Sleep(60000)
  51.  
  52.         End Try
  53.  
  54.     End Sub
  55. End Module

Problem Resolved
Put a dowhile loop
and put the thread sleep after end try
Jul 23 '07 #2

100+
P: 375
Problem Resolved
Put a dowhile loop
and put the thread sleep after end try
I have created a windows installer using VB.net 2005
(as per msdn link given below)
http://msdn2.microsoft.com/en-us/library/zt39148a(VS.80).aspx

Now I need to include the above said application inside the windows service.
How to do?
Jul 23 '07 #3

100+
P: 375
I have created a windows installer using VB.net 2005
(as per msdn link given below)
http://msdn2.microsoft.com/en-us/library/zt39148a(VS.80).aspx

Now I need to include the above said application inside the windows service.
How to do?
Problem Solved
Installed it in windows service
Jul 28 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.