Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi

I developed an application in c# using windows services to get a alert message for every 2 mintues .following is my code.

The problem with my code is many alert messages are coming instead of one alert message for every two minutes.How can I restrict to only one alert message.

Thanks in advance.
C#
protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

//This statement is used to set interval to 1 minute (= 60,000 milliseconds)

timer.Interval = 60000*2;

//enabling the timer
timer.Enabled = true;
}

protected override void OnStop()
{
timer.Enabled = false;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
while (true)
{
MessageBox.Show("hello there!");
System.Threading.Thread.Sleep(60 * 2000);
}
}
Posted
Updated 13-Jun-14 8:51am
v2
Comments
LLLLGGGG 13-Jun-14 15:04pm    
Hi,

The approach with a while(true) is not so good... In any case except for applications which aim is to execute a piece of code as fast as they can (like videogames).

I think you shouldn't use the while true as well as the Thread.Sleep method. What you need is only to make timer (and, if I have not misunderstood, it is a System.Timer) go without making any controls on the event handler.

I have a question about your code: have you created a derived class from System.Timer? However, what are you overriding, which class does it derive from?

Thanks.
goathik 13-Jun-14 15:10pm    
yeah, i also got curious about that override
LLLLGGGG 13-Jun-14 15:14pm    
I would suggest to do that:

 
protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

//This statement is used to set interval to 1 minute (= 60,000 milliseconds)

timer.Interval = 60000*2;

//enabling the timer
timer.Enabled = true;
}

protected override void OnStop()
{
timer.Enabled = false;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
MessageBox.Show("hello there");
}


But without any other information, I cannot say if it will work for sure...
However, I've made something similar some months ago... I've put my application in my automatic execution folder.
[no name] 13-Jun-14 18:35pm    
You should not be showing any windows from a windows service anyway.

1 solution

Isn't your problem obvious? Your event handler method is already supposed to be called periodically each two minuted, but additionally, you also do this loop with 2-minute sleep, which, by the way, totally blocks the calling thread, creating impossible mess. I cannot even imagine what reasoning could possibly lead to such weird code. Wouldn't the mere fact having of two immediate constants with the same values 60000*2 and 60 * 2000 give you a hint? Just remove this loop.

But then, you will have modal dialog box popping up every two minutes. If the user is not closing them all the time, they will stack up on top, more and more of them, eventually depleting all the system resource. The whole idea does not deserve consideration. Better think of doing something useful.

—SA
 
Share this answer
 
v2
Comments
LLLLGGGG 13-Jun-14 15:21pm    
It would be useful to define a bool global variable (such as "message-shown"), set it to true when the modal dialog is shown and reset it to false after the dialog has closed (if you put the first instruction just before the msgbox call and the second after, the execution of the main thread will stop at the msgbox line until it'll be closed). Then, wrap this lines into an if block which controls if the variable is false, and, if it is, execute the code, unless do not execute anything! I'm about to update my codesnippet above...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900