473,729 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer + High CPU Usage

LBT
I have a window service written using VB.NET. This window service will scan
folders for file and grab the file content to be inserted to SQL Server on
file detection. There are altogether 18 folders to be watched. Each folder is
assigned with a timer for watching purpose. Hence there will be 18 timers and
each timer is set to elapse on every second.

Problem here, once the window service is installed and started, the CPU
usage is very high (take about 10% of overall CPU usage at a four 2GHz
processors server even when there is no file being sent in for processing).
This highly comsumption of CPU power is because of the timers elapsed event?
Any idea to improve it? Any advice and comment would be much appreciated.
Nov 21 '05 #1
7 4980
On 2004-12-01, LBT <LB*@discussion s.microsoft.com > wrote:
I have a window service written using VB.NET. This window service will scan
folders for file and grab the file content to be inserted to SQL Server on
file detection. There are altogether 18 folders to be watched. Each folder is
assigned with a timer for watching purpose. Hence there will be 18 timers and
each timer is set to elapse on every second.

Problem here, once the window service is installed and started, the CPU
usage is very high (take about 10% of overall CPU usage at a four 2GHz
processors server even when there is no file being sent in for processing).
This highly comsumption of CPU power is because of the timers elapsed event?
More then likely what your doing in the event... How often are the
timers firing? Which timer class are you using?
Any idea to improve it? Any advice and comment would be much appreciated.


Have you looked at the FileSystemWatch er class in System.IO. You
shouldn't need timers and polling this way...

--
Tom Shelton [MVP]
Nov 21 '05 #2
LBT
Thanks for the reply.

I have used the Timer under "Components " tab located in the .NET IDE Toolbox.

I did use the FileSystemWatch er class before but the class somehow have
limitation. Whenever SQL Server is down or offline, the file that being
halfway processed or subsequent file sent in to the folder will not be
processed as FileSystemWatch er only provides method such as OnCreated,
OnModified, OnDeleted, etc. The file will be stuck inside the folder without
processed after all. Hence I have changed the mechanism by always scanning
folder for file using timer.

"Tom Shelton" wrote:
On 2004-12-01, LBT <LB*@discussion s.microsoft.com > wrote:
I have a window service written using VB.NET. This window service will scan
folders for file and grab the file content to be inserted to SQL Server on
file detection. There are altogether 18 folders to be watched. Each folder is
assigned with a timer for watching purpose. Hence there will be 18 timers and
each timer is set to elapse on every second.

Problem here, once the window service is installed and started, the CPU
usage is very high (take about 10% of overall CPU usage at a four 2GHz
processors server even when there is no file being sent in for processing).
This highly comsumption of CPU power is because of the timers elapsed event?


More then likely what your doing in the event... How often are the
timers firing? Which timer class are you using?
Any idea to improve it? Any advice and comment would be much appreciated.


Have you looked at the FileSystemWatch er class in System.IO. You
shouldn't need timers and polling this way...

--
Tom Shelton [MVP]

Nov 21 '05 #3
In article <6F************ *************** *******@microso ft.com>, LBT wrote:
Thanks for the reply.

I have used the Timer under "Components " tab located in the .NET IDE Toolbox.

I did use the FileSystemWatch er class before but the class somehow have
limitation. Whenever SQL Server is down or offline, the file that being
halfway processed or subsequent file sent in to the folder will not be
processed as FileSystemWatch er only provides method such as OnCreated,
OnModified, OnDeleted, etc. The file will be stuck inside the folder without
processed after all. Hence I have changed the mechanism by always scanning
folder for file using timer.


You should still be able to use filesystemwatch er... You simply need to
queue failed requests - then you would only need one timer. That timer
would check periodically to see if there were files that needed to be
processed in the failed queue.

private sub watcher_Created (byval sender as object, byval e as
filesystemevent args e)

try
attempt to process file

catch ex as exception
we couldn't process - so
failedqueue.add (file)
end try
end sub

private sub timer_elapsed (....)
try
attempt to process files in queue
catch ex as excpetion
add current file back to queue
failedqueue.add (file)
end try
end sub

make sense?

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 39 Days, 19 Hours, 23 Minutes, 47 Seconds
Nov 21 '05 #4
LBT
Appreciated for the suggestion :) But I still have doubt and wish that Tom
could provide me some valuable idea.

I think I will explain some logic flow of my window service in order to have
a better understanding to cope my hassle.

The window service needs to process files from 16 directories. Each folder
has its own file format. There will be no limitation on the number of files
that can be sent in to a folder at one time. But only 1 file is allowed to
process at one time (sequence of file based on creation datetime is very
important). I might need a queue table to stack the records (file info)
before each file get its turn to be processed. The role of the window service
is to obtain the file content and insert to a SQL table as a record. In the
SQL table, there is a INSERT trigger which will be invoked and execute a
stored procedure to carry out validation towards the record. At the last
portion of the stored procedure, it will update the queued record to
"FINISH". When WS detect for this "FINISH" status, it will move the file to
archive folder and processing for 1 file is considered finish.

Question here, if I use the FileSystemWatch er class, it will help me to put
the file info to the queue table with the help of OnCreated event. But I
might still need a timer to process the queue table to ensure that file is
processed in sequence and only one file is processed at one time.

Currently, I use a timer to scan directory. If no file is detected, the
process will stop and wait for next elapsed event. If file(s) detected, all
file info as grabbed at that time will be put to a queue table. All record(s)
inside the queue table should be finish processed before subsequent file
exists in a folder would be catered again. In order words, the timer elapsed
event will not scan directory for file until queue table is finish processed
(controlled using a boolean flag).

As what can be seen, timer should be there for first method
(FileSystemWatc her + Timer) and second method which I used for this moment.
Timer seem can't be escaped from using for both methods and the use of timer
would again increase the CPU usage when all timers elapsed at the same time.
For yuor information, timer interval is set to 1 second.

Any way I can improve so that the CPU usage will not reach the critical
level? What I've tested shows that the CPU usage is considered high when
there is no file being sent in to any of the folders. The CPU usage will
become higher when there is file sent in for processing. Any weakness in my
existing file processing mechanism? Any better workaround?

Thanks a lot


Nov 21 '05 #5
Uset the FileSystemWatch er to tell you when a new file has been created.
Then use the same scanning algorithm you currently use. By using the
FileSystemWatch er, you eliminate both excessive scans and delayed scans.
Once you have completed scanning, then restart start the FileSystemWatch er.

Mike Ober.

"LBT" <LB*@discussion s.microsoft.com > wrote in message
news:78******** *************** ***********@mic rosoft.com...
Appreciated for the suggestion :) But I still have doubt and wish that Tom
could provide me some valuable idea.

I think I will explain some logic flow of my window service in order to have a better understanding to cope my hassle.

The window service needs to process files from 16 directories. Each folder
has its own file format. There will be no limitation on the number of files that can be sent in to a folder at one time. But only 1 file is allowed to
process at one time (sequence of file based on creation datetime is very
important). I might need a queue table to stack the records (file info)
before each file get its turn to be processed. The role of the window service is to obtain the file content and insert to a SQL table as a record. In the SQL table, there is a INSERT trigger which will be invoked and execute a
stored procedure to carry out validation towards the record. At the last
portion of the stored procedure, it will update the queued record to
"FINISH". When WS detect for this "FINISH" status, it will move the file to archive folder and processing for 1 file is considered finish.

Question here, if I use the FileSystemWatch er class, it will help me to put the file info to the queue table with the help of OnCreated event. But I
might still need a timer to process the queue table to ensure that file is
processed in sequence and only one file is processed at one time.

Currently, I use a timer to scan directory. If no file is detected, the
process will stop and wait for next elapsed event. If file(s) detected, all file info as grabbed at that time will be put to a queue table. All record(s) inside the queue table should be finish processed before subsequent file
exists in a folder would be catered again. In order words, the timer elapsed event will not scan directory for file until queue table is finish processed (controlled using a boolean flag).

As what can be seen, timer should be there for first method
(FileSystemWatc her + Timer) and second method which I used for this moment. Timer seem can't be escaped from using for both methods and the use of timer would again increase the CPU usage when all timers elapsed at the same time. For yuor information, timer interval is set to 1 second.

Any way I can improve so that the CPU usage will not reach the critical
level? What I've tested shows that the CPU usage is considered high when
there is no file being sent in to any of the folders. The CPU usage will
become higher when there is file sent in for processing. Any weakness in my existing file processing mechanism? Any better workaround?

Thanks a lot

Nov 21 '05 #6
LBT
Currently I used the System.IO.Direc tory.GetFiles method to scan directory.
Is it the method will cause lot of undesired overhead? The use of
FileSystemWatch er will be able to eliminate this?

Thanks

"Michael D. Ober" wrote:
Uset the FileSystemWatch er to tell you when a new file has been created.
Then use the same scanning algorithm you currently use. By using the
FileSystemWatch er, you eliminate both excessive scans and delayed scans.
Once you have completed scanning, then restart start the FileSystemWatch er.

Mike Ober.

"LBT" <LB*@discussion s.microsoft.com > wrote in message
news:78******** *************** ***********@mic rosoft.com...
Appreciated for the suggestion :) But I still have doubt and wish that Tom
could provide me some valuable idea.

I think I will explain some logic flow of my window service in order to

have
a better understanding to cope my hassle.

The window service needs to process files from 16 directories. Each folder
has its own file format. There will be no limitation on the number of

files
that can be sent in to a folder at one time. But only 1 file is allowed to
process at one time (sequence of file based on creation datetime is very
important). I might need a queue table to stack the records (file info)
before each file get its turn to be processed. The role of the window

service
is to obtain the file content and insert to a SQL table as a record. In

the
SQL table, there is a INSERT trigger which will be invoked and execute a
stored procedure to carry out validation towards the record. At the last
portion of the stored procedure, it will update the queued record to
"FINISH". When WS detect for this "FINISH" status, it will move the file

to
archive folder and processing for 1 file is considered finish.

Question here, if I use the FileSystemWatch er class, it will help me to

put
the file info to the queue table with the help of OnCreated event. But I
might still need a timer to process the queue table to ensure that file is
processed in sequence and only one file is processed at one time.

Currently, I use a timer to scan directory. If no file is detected, the
process will stop and wait for next elapsed event. If file(s) detected,

all
file info as grabbed at that time will be put to a queue table. All

record(s)
inside the queue table should be finish processed before subsequent file
exists in a folder would be catered again. In order words, the timer

elapsed
event will not scan directory for file until queue table is finish

processed
(controlled using a boolean flag).

As what can be seen, timer should be there for first method
(FileSystemWatc her + Timer) and second method which I used for this

moment.
Timer seem can't be escaped from using for both methods and the use of

timer
would again increase the CPU usage when all timers elapsed at the same

time.
For yuor information, timer interval is set to 1 second.

Any way I can improve so that the CPU usage will not reach the critical
level? What I've tested shows that the CPU usage is considered high when
there is no file being sent in to any of the folders. The CPU usage will
become higher when there is file sent in for processing. Any weakness in

my
existing file processing mechanism? Any better workaround?

Thanks a lot


Nov 21 '05 #7
Hi,

I made a form, adding this code

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim oTimer(19) As MyTimer
For I As Integer = 0 To 19
oTimer(I) = New MyTimer
oTimer(I).Inter val = 1000
oTimer(I).nID = I

AddHandler oTimer(I).Elaps ed, AddressOf oTimer_Elapsed
oTimer(I).Start ()
Next
End Sub

Public Sub oTimer_Elapsed( ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs )
Dim sArray() As String = System.IO.Direc tory.GetFiles(" C:\T\" &
CType(sender, MyTimer).nID & "\", "*.*")
If Not sArray Is Nothing Then
If sArray.Length > 0 Then
Debug.WriteLine ("Found file...")
End If
End If

End Sub

Public Class MyTimer
Inherits System.Timers.T imer

Private m_nID As Integer
Public Property nID() As Integer
Get
Return m_nID
End Get
Set(ByVal Value As Integer)
m_nID = Value
End Set
End Property
End Class

When running it (made 20 directories) it takes zero % processor power.

Also, a word of caution, a mistake that I made a while ago, if you create a
new timer and dont stop the old one (or run out of scope) it still elapes,
and when you have several thousand, the processor will start feeling it..
- Fredrik Melin
"LBT" <LB*@discussion s.microsoft.com > wrote in message
news:E4******** *************** ***********@mic rosoft.com...
I have a window service written using VB.NET. This window service will scan
folders for file and grab the file content to be inserted to SQL Server on
file detection. There are altogether 18 folders to be watched. Each folder
is
assigned with a timer for watching purpose. Hence there will be 18 timers
and
each timer is set to elapse on every second.

Problem here, once the window service is installed and started, the CPU
usage is very high (take about 10% of overall CPU usage at a four 2GHz
processors server even when there is no file being sent in for
processing).
This highly comsumption of CPU power is because of the timers elapsed
event?
Any idea to improve it? Any advice and comment would be much appreciated.

Nov 21 '05 #8

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

Similar topics

3
4146
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database periodically. What happens, is that the app reads the contents of a text file line by line into an ArrayList. Each element of the ArrayList is a string representing a record from the file. The ArrayList is then processed, and the arraylist goes out of...
3
7837
by: jam | last post by:
Dear all, I am wrtiing a console application and doing some test on timer, the below is my sample code I got from some site, but I cannot make it work... Error is 'System.Timers.Timer' does not contain a definition for 'Tick' So what should i do??? the final thing I wanna get is, I have a console will call a exe running in background, and then I wanna check it is is still running ( Use get process
2
733
by: Michael Evans | last post by:
First, we rely on a stable update rate so that our physics and dynamics calculations and integrations are based on a known interval and therefore are true-to-life. Second, the graphics positions in our image generators are updated by this dynamics application, and we need to update them at the rate they are being refreshed so that we don't get any stutter from frames being used twice or having to be thrown out. Without microsecond...
4
11125
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts the default of True. The Elapsed event handler updates the dialog box with how long before it will close, acting as a timer itself. The dialog has a time to close property which is checked every time the Elapsed event fires. The problem I have...
6
10243
by: paresh | last post by:
Hi all, I need to set timer in C/linux like alram, such that i will get a timeout signal after specific timeout and my process remain executing as is it. I can use signal(SIGALRM, xyz) and then alarm(some value in sec), there is a constraint in this as i can pass timeout only in seconds and i need in milli sec. Any idea how todo this.
10
12714
by: DaTurk | last post by:
Hi, I'm creating an application that will need to use a timer to do a static method call every second. My question, is what would be the best timer to use? I know there are several, but I'd like to use a timer that is probably the most reliable, and hopefully designed with high performance in mind. Thank you in advance.
9
3115
by: =?Utf-8?B?TWlrZTk5MDA=?= | last post by:
Hello, I am wondering if it is a good idea to use GC.Collect() in a timer. For example, timer is fired every 5 minutes and calls GC.Collect(). In our server app the memory goes to 600MB and stays there. The memory hardly goes down. When I disconnect the client and the connect it again the memory could reclaim to 400MB. But it could go down to 50 MB.
7
502
by: Andrew Wan | last post by:
I found this excellent High Speed Timer (in Pascal). I compiled it (using Turbo Pascal 7 and it runs fine): http://www.sorucevap.com/bilisimteknolojisi/programcilik/pascal/ders.asp?207995 and same High Speed Timer here too: http://groups.google.com/group/comp.lang.pascal/browse_thread/thread/92e9398f16c10ba4/e67ff3cf587648ef?lnk=st&q=inline(%24CD)+inline(%241C)+inline(%249C)&rnum=1&hl=en#e67ff3cf587648ef I converted it to C (using p2c),...
6
2982
by: dantz | last post by:
HI everyone, I hope someone can help me on this. I have form application that has 3 Timers that does an animation (changing an image for every interval) Each image are loaded at start of application and saved into a Dictionary<string, List<Image>> The dictionary will have 5 items which is for 5 controls. then each control will have this images in the dictionary(I also assigned the timers):
0
8928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8766
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9217
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9158
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3244
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2698
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.