473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

System.Timer Question

Hi.

I Have a VB.Net Windows Application that starts up using Sub Main.
It's a program that runs in the background and checks something every minute.
I use a Timer to accomplish this and it's working but to get it to work I
use a boolean variable (mbLoop) and the program runs until mbLoop is set to
False.
Do While mbLoop
'
Loop

The problem is that I see this program always using 25% of CPU. I know it's
the looping that's going on that's causing this but I don't know how to keep
the program running without it. I tried taking it out but it just executed
the code in Sub Main and when it hit End Sub the program ended.

Here's my code:
Imports System.Threading 'Timer
Module modMain
Dim WithEvents moTimer As System.Timers.Timer
Sub Main()
moTimer = New System.Timers.Timer(60000)

moTimer.AutoReset = True

moTimer.Enabled = True

'*** Will continue working as long as mbLoop is set to True
Do While mbLoop
'
Loop
End Sub

Sub OnTimedEvent(ByVal Source As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
Handles
moTimer.Elapsed
'
' All my code here is executed every 60 seconds
'
End Sub

Any suggestions will be greatly appreciated!

Rita

Nov 22 '05 #1
3 3288
Hi RitaG,

To free up resources while being in a loop I allways put the thread to sleep.
System.Threading.Thread.Sleep(myInteger);

Kind regards,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"RitaG" wrote:
Hi.

I Have a VB.Net Windows Application that starts up using Sub Main.
It's a program that runs in the background and checks something every minute.
I use a Timer to accomplish this and it's working but to get it to work I
use a boolean variable (mbLoop) and the program runs until mbLoop is set to
False.
Do While mbLoop
'
Loop

The problem is that I see this program always using 25% of CPU. I know it's
the looping that's going on that's causing this but I don't know how to keep
the program running without it. I tried taking it out but it just executed
the code in Sub Main and when it hit End Sub the program ended.

Here's my code:
Imports System.Threading 'Timer
Module modMain
Dim WithEvents moTimer As System.Timers.Timer
Sub Main()
moTimer = New System.Timers.Timer(60000)

moTimer.AutoReset = True

moTimer.Enabled = True

'*** Will continue working as long as mbLoop is set to True
Do While mbLoop
'
Loop
End Sub

Sub OnTimedEvent(ByVal Source As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
Handles
moTimer.Elapsed
'
' All my code here is executed every 60 seconds
'
End Sub

Any suggestions will be greatly appreciated!

Rita

Nov 22 '05 #2
Thanks Rainier.

I was looking at the Sleep method without the loop but don't know which thread
to "put to sleep". I'll read up on it now.

Thanks for your response.
Rita

"Rainier [MCT]" wrote:
Hi RitaG,

To free up resources while being in a loop I allways put the thread to sleep.
System.Threading.Thread.Sleep(myInteger);

Kind regards,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"RitaG" wrote:
Hi.

I Have a VB.Net Windows Application that starts up using Sub Main.
It's a program that runs in the background and checks something every minute.
I use a Timer to accomplish this and it's working but to get it to work I
use a boolean variable (mbLoop) and the program runs until mbLoop is set to
False.
Do While mbLoop
'
Loop

The problem is that I see this program always using 25% of CPU. I know it's
the looping that's going on that's causing this but I don't know how to keep
the program running without it. I tried taking it out but it just executed
the code in Sub Main and when it hit End Sub the program ended.

Here's my code:
Imports System.Threading 'Timer
Module modMain
Dim WithEvents moTimer As System.Timers.Timer
Sub Main()
moTimer = New System.Timers.Timer(60000)

moTimer.AutoReset = True

moTimer.Enabled = True

'*** Will continue working as long as mbLoop is set to True
Do While mbLoop
'
Loop
End Sub

Sub OnTimedEvent(ByVal Source As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
Handles
moTimer.Elapsed
'
' All my code here is executed every 60 seconds
'
End Sub

Any suggestions will be greatly appreciated!

Rita

Nov 22 '05 #3
FYI.

After doing some research I was able to remove the loop and just added this
line instead " Thread.Sleep(System.Threading.Timeout.Infinite)".

"RitaG" wrote:
Thanks Rainier.

I was looking at the Sleep method without the loop but don't know which thread
to "put to sleep". I'll read up on it now.

Thanks for your response.
Rita

"Rainier [MCT]" wrote:
Hi RitaG,

To free up resources while being in a loop I allways put the thread to sleep.
System.Threading.Thread.Sleep(myInteger);

Kind regards,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"RitaG" wrote:
Hi.

I Have a VB.Net Windows Application that starts up using Sub Main.
It's a program that runs in the background and checks something every minute.
I use a Timer to accomplish this and it's working but to get it to work I
use a boolean variable (mbLoop) and the program runs until mbLoop is set to
False.
Do While mbLoop
'
Loop

The problem is that I see this program always using 25% of CPU. I know it's
the looping that's going on that's causing this but I don't know how to keep
the program running without it. I tried taking it out but it just executed
the code in Sub Main and when it hit End Sub the program ended.

Here's my code:
Imports System.Threading 'Timer
Module modMain
Dim WithEvents moTimer As System.Timers.Timer
Sub Main()
moTimer = New System.Timers.Timer(60000)

moTimer.AutoReset = True

moTimer.Enabled = True

'*** Will continue working as long as mbLoop is set to True
Do While mbLoop
'
Loop
End Sub

Sub OnTimedEvent(ByVal Source As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
Handles
moTimer.Elapsed
'
' All my code here is executed every 60 seconds
'
End Sub

Any suggestions will be greatly appreciated!

Rita

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer...
1
by: melanieab | last post by:
Hi, I'm have a datagrid, and I'm trying to have a tooltip pop up if a cell has been hovered on for 2 seconds. I was thinking of using DataGrid.Hover, but then decided to try this instead: ...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
9
by: archana | last post by:
Hi all, I want to know about interval of timer. I am using timer in windows service.I head somewhere that when i set interval property of timer while setting interval, restart time of Pc is...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.