5 Replies - 823 Views - Last Post: 24 January 2015 - 01:34 PM Rate Topic: -----

#1 ehgan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-January 15

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.

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


Is This A Good Question/Topic? 0
  • +

Replies To: How to set the time and run threading in VB.NET

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1545
  • View blog
  • Posts: 3,871
  • Joined: 01-February 13

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.

    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


Was This Post Helpful? 2
  • +
  • -

#3 ehgan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-January 15

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?
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1545
  • View blog
  • Posts: 3,871
  • Joined: 01-February 13

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)

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


Was This Post Helpful? 1
  • +
  • -

#5 ehgan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-January 15

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

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 .
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1545
  • View blog
  • Posts: 3,871
  • Joined: 01-February 13

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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1