views:

756

answers:

4

So I'm kind of new to VB and am just playing around with a little project, I currently need a loop that is constantly checking the systems clock to see if it's equal to a certain time.

   While Not myTime.Hour = 24

        If TimeOfDay = newTime Then
            nfi.ShowBalloonTip(15)
            intRandNumb = RandomNumber(1, 15)
            dblAddMinutes = intTime + intRandNumb
            newTime = TimeOfDay.AddMinutes(dblAddMinutes)

        End If
    End While

I have this right now, but obviously it's grinding everything to a halt and using 50% of my cpu in the process, I just would like to know what I can substitute in or change to make this loop run better and perform how I need it to.

+1  A: 

you can add

Threading.Thread.Sleep(0),

this will cause a context switch and greatly reduce the CPU usage

Also consider using a timer object to be called every 10 or 100 ms, this will also be better in usage then having a loop

Am
I've never used a timer object before how would I go about implementing one in this case?
LeSabo
see @Grizzly on how to implement the Timer solution
Am
+1  A: 

You can use

Threading.Thread.Sleep(0)

This will cause the working thread to yield the rest of it's current timeslice which will reduce the cpu usage quite a bit. However you should consider whether you really nead busy waiting for the time or if you could get away with setting a timer to count down the difference between the current time and the expected time, e.g.:

            var t = new System.Timers.Timer((DateTime.Now - DateTime.Now).TotalMilliseconds);
        t.Elapsed = DoSomething;
        t.Start();
Grizzly
so if I'm reading that correctly that creates a timer in milliseconds of the difference between the two times, and does something once the timer has elapsed? I apologize, I've never had to use a timer before.
LeSabo
yes, that is exactly the intended behaviour (of course this code is c#, so there are small syntactic differences when using it in vb.net, but other then that...)
Grizzly
of course, wow, thanks a lot for your help. You taught me some good stuff in a short time.
LeSabo
+1  A: 

checking the systems clock to see if it's equal to a certain time.

There are two "correct" ways to do this:

  1. Build a normal app that doesn't care what time it is, and set it up in windows as a schedule task.
  2. Check the time once and calculate how long until the desired time. Then set up a timer to wait for that exact duration.

Under no circumstance should you keep polling the system clock for something like this that will just run once.

Joel Coehoorn
based on the code sample in the question, I'd say option 2 is more correct! And I've often used this method myself.
A: 

As Joel pointed out, you should try using a timer instead. I'm not sure if your app is a form or console or other, so I'll try to be generic and use System.Timers.Timer.

The code here (interval is set at 10ms, change to a value of your need):

Private timer1 As System.Timers.Timer    
Const interval As Integer = 10 


Sub initTimer()
    timer1 = New System.Timers.Timer(10)
    AddHandler timer1.Elapsed, AddressOf Me.timer_Elapsed
    timer1.Start()
End Sub

Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    'do your stuff here
    'Console.WriteLine(e.SignalTime.ToString())
End Sub
o.k.w
OOps, I didn't realise Grizzly did a sample. Mine is VB though. I'm a C#-er, so took me awhile to get the codes out.
o.k.w
Thanks for your time, it really helped and everything is running smoothly now.
LeSabo