Dim MyThreading As Threading.Thread
Public Sub StartThread()
MyThreading = New Threading.Thread(AddressOf DoTheThreads)
MyThreading.Start()
End Sub
Sub DoTheThreads()
Dim timE As Date = Date.Now
Dim CurrentHour As Integer = timE.Hour
Dim CurrentMin As Integer = timE.Minute
Dim ReportHr As Integer = 12
Dim ReportMin As Integer = 30
If CurrentHour = ReportHr AndAlso CurrentMin = ReportMin Then
MessageBox.Show("Hello World")
End If
Threading.Thread.Sleep(1000)
End Sub
How to set the time and run threading in VB.NET
Page 1 of 15 Replies - 823 Views - Last Post: 24 January 2015 - 01:34 PM
#1
How to set the time and run threading in VB.NET
Posted 24 January 2015 - 10:37 AM
I want to set the time at 12:30 pm and run my thread without using timer. How can I do this? Below is my code, but this is not working when the system time and time set are the same.
Replies To: How to set the time and run threading in VB.NET
#2
Re: How to set the time and run threading in VB.NET
Posted 24 January 2015 - 11:31 AM
Hi,
I don`t know if you realized it but, the way you have the code set up, when you start the thread it will run through the code just once and if it is not the correct time then the thread is exited.
If you want to set it up so you can just start the thread and let it run until the correct time is reached then you need to use a loop to keep checking the time until it reaches the correct time.
Also, i am not sure if you realized that the hours of the date are in a 24 hour format, like if it is 1:00 PM it will give the hour as 13. Just thought i would mention that in case you wanted to use different times than 12:30 PM.
I don`t know if you realized it but, the way you have the code set up, when you start the thread it will run through the code just once and if it is not the correct time then the thread is exited.
If you want to set it up so you can just start the thread and let it run until the correct time is reached then you need to use a loop to keep checking the time until it reaches the correct time.
Also, i am not sure if you realized that the hours of the date are in a 24 hour format, like if it is 1:00 PM it will give the hour as 13. Just thought i would mention that in case you wanted to use different times than 12:30 PM.
Private Sub DoTheThreads()
Dim timE As Date = TimeOfDay
Dim ReportHr As Integer = 12
Dim ReportMin As Integer = 30
While timE.Hour <> ReportHr Or timE.Minute <> ReportMin
Threading.Thread.Sleep(5000)
timE = Now
End While
MessageBox.Show("Hello World")
End Sub
#3
Re: How to set the time and run threading in VB.NET
Posted 24 January 2015 - 12:39 PM
thanks man, sorry for late reply but when I run your code . the message box show "Hello World". I set the time 1 min before I run the program but message box show even they did not reached the time yet. What do you think were is the problem?
#4
Re: How to set the time and run threading in VB.NET
Posted 24 January 2015 - 12:52 PM
I am not sure. Did you copy my code exactly as i posted it?
Below is the complete code i tested this with and it seems to wait until the correct time before showing the messagebox on my end. Compare that with what you have and see if there is a difference.
If you cant find a problem then you will need to post the code that you currently have to see if someone can find a problem with it. 8)
Below is the complete code i tested this with and it seems to wait until the correct time before showing the messagebox on my end. Compare that with what you have and see if there is a difference.
If you cant find a problem then you will need to post the code that you currently have to see if someone can find a problem with it. 8)
Public Class Form1
Dim MyThreading As Threading.Thread
Private Sub StartThread()
MyThreading = New Threading.Thread(AddressOf DoTheThreads)
MyThreading.Start()
End Sub
Private Sub DoTheThreads()
Dim timE As Date = TimeOfDay
Dim ReportHr As Integer = 14 '2
Dim ReportMin As Integer = 44 '44
While timE.Hour <> ReportHr Or timE.Minute <> ReportMin
Threading.Thread.Sleep(5000)
timE = Now
End While
MessageBox.Show("Hello World")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
StartThread()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MyThreading IsNot Nothing AndAlso MyThreading.IsAlive Then MyThreading.Abort()
End Sub
End Class
#5
Re: How to set the time and run threading in VB.NET
Posted 24 January 2015 - 01:09 PM
This is Great ! Your my Savior Man 
Can you give me some advice I am only beginner in vb.net.
Actually I use Time Set to Backup my database using mysqldump, So if ever I will put my sqldump codes under the end of while loop. Do you think my idea is correct ? please give me some advice. Thank you once again man .
Can you give me some advice I am only beginner in vb.net.
Actually I use Time Set to Backup my database using mysqldump, So if ever I will put my sqldump codes under the end of while loop. Do you think my idea is correct ? please give me some advice. Thank you once again man .
#6
Re: How to set the time and run threading in VB.NET
Posted 24 January 2015 - 01:34 PM
I am not familiar with databases and SQL so, i am afraid i will have to let someone else answer that one. Sorry i can`t help with that.
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|