Can I make my program more efficient ?

G

Guest

Hi;

I have designed a vb.net program that reads a dateTime value from a database
table and then compares it to Now() through dateTime.compare(). I have loaded
an array with my datetime values from my table and I loop through this until a
match is made and then I run another program. Since I am continously checking
the system for a specific time the CPU stays at 99 %. I tried adding a do
loop and a timer to try to slow down the time between iterations of my
comparison loop but I still don't have the time right.

Here is my code if it's helps:

Do
For y = 0 to Len(holdDateTime_Start(y))
If dateTime.Compare(holdDateTime_start(y), Now) = 0 then
processJobs(holdJobName(y))
end if
Exit For
next

initializeTimer()

Do
Loop until Timer1.interval = 0

y = 0

Loop Until dateTIme.Compare("08/31/2006 12:00:00 AM", Now) = 0


Yes, I am trying to build my on task sceduler since the windows version
doesn't fit my needs.

Thanks for your suggestions.
 
C

crazyone

Simple suggestion, create a timer that responds to a certain TICK
frequency. That way, you let the computer manage the workload and only
get to run your code every X millisecond.

If a millisecond is not precise enough then i suggest searching for
another feature but i sincerely doubt that will be too long... With a
timer set to 1 second interval, the processor will not loop at 99%
constantly at least.

Math

PS: What makes you think the task scheduler wouldn't work? What feature
are you looking for cause it's quite strong and polyvalent...
 
G

Guest

Why won't the task sceduler fit my needs ?

I did a little research into programming some of it's objects, I happened
to notice
the daily trigger example which would meet some of my needs but I notice
that one of the requirements is Windows Vista. I work for a financial
institution and it will probably be a couple of years after the mainstream
uses Vista before we adopt it.

Is there support for dealing with the task scheduler already in VS 2003 ?

Thanks for your answer did you say to look at manipulating the TickFrequency
property ?
 
C

crazyone

No i simply meant to use a control on a form such as a timer and set
the frequency in seconds... that would allow you to check the time each
second without having to rely on loop endlessly and checking millions
of times a second.
 
P

Pritcham

Hi

If you're simply trying to compare Now() with existing entries in the
DB then why not do the filtering in your DB call.

i.e. do a "Select * from mytable Where myTimeField = Now()" or
GetDate() if more appropriate. That way you don't have to loop through
all the records each time as you'll only get matching records returned.
If you're concerned about 'missing' any of the DB values due to time
lapses between your DB calls then add a "Between Now() and Now()+1
minute" or whatever.

Hope that helps
 
M

Mudhead

Here is a simple soultion to your problem. There's no error handling. It's
just a quick sample.

Public Class Form1
Dim value(3) As DateTime
Dim index As Integer = 0
Dim dt As TimeSpan
Dim Timer2 As New System.Timers.Timer()

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

value(0) = CDate("8/14/2006 11:12:47 AM")
value(1) = CDate("8/14/2006 11:13:27 AM")
value(2) = CDate("8/14/2006 11:13:33 AM")
value(3) = CDate("8/14/2006 11:13:59 AM")
dt = value(0) - Now

CreateTimer()

End Sub

Private Sub CreateTimer()

Timer2.Interval = dt.TotalMilliseconds
Timer2.Enabled = True
AddHandler Timer2.Elapsed, _
New System.Timers.ElapsedEventHandler(AddressOf
Me.Timer1_Elapsed)
End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)

index += 1
Debug.WriteLine("Message " & index & ": " & Now)
If index < 4 Then
dt = value(index) - Now
Timer2.Interval = dt.TotalMilliseconds
Else
Timer2.Enabled = False
Debug.WriteLine("Disabled")
End If
End Sub
End Class
 
G

Guest

Hi ;

Thanks for all for your suggestions.

I also posted my question with the visual studio general newsgroup and I got
a very simple solution.

Place this statement just outside my next statement :

Threading.Thread.Sleep(number of milliseconds to pause).

This works just as well as the windows.timer.
 
M

Michael D. Ober

The difference between the two options are that the thread.sleep statement
forces the current thread to stop executing during the sleep. In many cases
this is fine. The timer solution instructs Windows to alert your program
after a specified interval that it can do something. The calling thread
doesn't stall, but you get the added complexity of a multi-threaded
application. Sometimes this is what you want.

Mike Ober.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top