473,769 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accute Timer with High Processor Load

Hi all,

I'm writing a .net Com object to read messages from an external bus.
Also, under certain situations, I output messages to the bus in order
to generate bus load, for testing purposes. An idea of my code is
supplied below, and as you can see I use Stopwatch to generate an
accurate timer. DoLoop is my timer callback and it is called with an
interval of 20ms. When doing the bus load I need the timer to perform
as well as possible, so I use Sleep(0) while polling the stopwatch. As
you can imagine, this sends the processor load to 100%. When not doing
the bus load, I use Sleep(1). This results in the usual approx. 15ms
sleep, depending on the system. This is fine - at the moment at
least...

So I have tried 3 variations of my code -

1. The code below works well, but when this.busLoad.If Started() is
true, the computer is very non responsive.

2. I've also tried using Sleep(1) while also doing the bus load.
Result: Bus load is less reliable as messages are not outputted as
periodically as in 1 above.

3. Keeping the thread priority at normal while using Sleep(0). This
works well, except when I start doing things with other applications
etc. and then I start to see the same behavior as in 2.

Anyway, I am wondering if my approach is a good one with regard to the
way I am implementing my callback timer. Can you think of any issues
based on this implementation - I'm aware that my timer will not run
exactly periodically, but it would be nice to run it as periodically
as is possible. One other idea I had was to change the thread priority
depending on how soon the it was until the callback was to be called.
Maybe that would be complete overkill.

using System;
using System.Runtime. InteropServices ;
using System.Diagnost ics;
using System.Threadin g;
using System.Collecti ons.Generic;

namespace Mine
{
public class Main
{
Thread timerLoop;
Stopwatch watch = new Stopwatch();
bool keepTiming = true;
int interval;

public Main(int interval)
{
this.interval = interval;
timerLoop = new Thread(new ThreadStart(DoL oop));
timerLoop.Prior ity =
System.Threadin g.ThreadPriorit y.Normal;
timerLoop.Start ();

timerLoop.Prior ity =
System.Threadin g.ThreadPriorit y.Highest;
}

/// <summary>
/// Stop Timer
/// </summary>

public void Stop()
{
keepTiming = false;
timerLoop.Join( );
}

private void DoLoop()
{
int threadSleepTime = 0;

while (keepTiming)
{
watch.Reset();
watch.Start();

if (this.busLoad.I fStarted()==tru e)
{
this.busLoad.Ge nerateBusLoad() ;
threadSleepTime = 0;
}
else
threadSleepTime = 1;

// Call Read Message Function

while (watch.ElapsedM illiseconds < interval)
{
Thread.Sleep(th readSleepTime);
}
}
}
}
}

Feb 21 '07 #1
0 1756

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

Similar topics

10
2708
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have two forms (frmSplash and frmMain) and a code Module setup as my startup object. Here is the code that I have, when I try to run this part of the splash screen form shows and then program terminates. What am I doing wrong? Thanks,
7
4986
by: LBT | last post by:
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...
2
4065
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: The MyService service on Local Computer started and then stopped. Some services stop automatically if they ahve no work to do, for example, the Perforamnce Logs and Alert service. I tried switching to a System.Threading.Timer and that didn't work...
4
11129
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
10246
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.
4
5407
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High resolution means that expire time should be measured in microseconds, or at least in milliseconds. I had a look on Gnu C <sys/time.h>'s functions getitimer and setitimer, but these all use a signal (SIGALRM) to notify expiration, and I don't know the...
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),...
19
5210
by: colin | last post by:
Hi, Im trying to time some parts of my code using the Stopwatch class, but it is so slow, I tried the QueryPerformanceFrequency() but this seems to be just as slow, if I just simply call this function in a loop it takes 21 ticks with a reported frequency of 3.5mhz its is about 6us wich is over 12k instructions on my 2ghz cpu. this negates the idea of having a high res timer ...
7
1997
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
0
9422
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
10208
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...
0
10038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
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
9857
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...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.