在控制台应用程序中等待计时器事件? [英] Waiting for a timer event in a console application?

查看:54
本文介绍了在控制台应用程序中等待计时器事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我设计了一个使用System.Timers.Timer

及其Elapsed事件的控制台应用程序。

问题就是我希望程序继续运行,以便每次引发事件时,处理程序都会被执行。

我尝试使用一段时间(timer.Enabled) ){}循环,但它使进程

在任务管理器中使用90%的cpu。什么是等待

事件并在控制台应用程序中保持程序活着的正确方法?

谢谢,

Aviv。

解决方案

avivgur,


您需要一种方法来阻止当前线程而不执行处理

在该线程中。我要做的是创建一个事件,并等待该事件的句柄

。然后,我会有另一个线程等待来自计时器的通知

。当通知进来时,你可以设置事件,

并让主线程继续处理。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" avivgur" < AV ***** @ gmail.com>在消息中写道

新闻:2c ************************* @ posting.google.co m ... < blockquote class =post_quotes>你好,
我设计了一个使用System.Timers.Timer
及其Elapsed事件的控制台应用程序。
问题是我希望程序继续运行以便每次引发事件时,处理程序都会被执行。
我尝试使用while(timer.Enabled){}循环,但它使进程使用了​​90%任务管理器中的cpu。在控制台应用程序中等待事件并使程序保持活动状态的正确方法是什么?
谢谢,
Aviv。





只需在你的循环中插入一个System.Threading.Thread.Sleep(1000)。

整数是毫秒,所以调整数量以适应你的应用...


大概是timer.Enabled被事件处理程序改变了?否则你将会b / b
永远不会离开循环!

" avivgur" < AV ***** @ gmail.com>在消息中写道

新闻:2c ************************* @ posting.google.co m ... < blockquote class =post_quotes>你好,
我设计了一个使用System.Timers.Timer
及其Elapsed事件的控制台应用程序。
问题是我希望程序继续运行以便每次引发事件时,处理程序都会被执行。
我尝试使用while(timer.Enabled){}循环,但它使进程使用了​​90%任务管理器中的cpu。在控制台应用程序中等待事件并使程序保持活动状态的正确方法是什么?
谢谢,
Aviv。



我认为他想做的只是等待计时器。怎么样呢

Thread.Sleep()?


Thomas P. Skinner [MVP]


" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

avivgur,

你需要一种方法来阻止当前线程而不在该线程中执行
处理。我要做的是创建一个事件,并等待该事件的句柄。然后,我会有另一个线程等待来自计时器的通知。当通知进来时,你可以设置事件,让主线程继续处理。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

" avivgur" < AV ***** @ gmail.com>在消息中写道
新闻:2c ************************* @ posting.google.co m ...

你好,我已经设计了一个控制台应用程序,它使用System.Timers.Timer
及其Elapsed事件。
问题是我希望程序继续运行,以便每个事件被引发的时间,处理程序都会被执行。
我尝试使用while(timer.Enabled){}循环,但它使进程在任务中使用90%的cpu经理。在控制台应用程序中等待事件并使程序保持活动状态的正确方法是什么?
谢谢,
Aviv。




Hello,
I have devised a console application that uses a System.Timers.Timer
and its Elapsed event.
The problem is that I want the program to continue to run so that each
time the event is raised, the handler will be executed.
I tried using a while(timer.Enabled) {} loop but it made the process
use 90% cpu in the task manager. What is the correct way to wait for
an event and keep the program alive in a console application?
Thanks,
Aviv.

解决方案

avivgur,

You need a way to block the current thread without performing processing
in that thread. What I would do is create an event, and wait on the handle
for that event. Then, I would have another thread wait on the notification
from the timer. When the notification comes in, then you can set the event,
and let the main thread continue processing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"avivgur" <av*****@gmail.com> wrote in message
news:2c*************************@posting.google.co m...

Hello,
I have devised a console application that uses a System.Timers.Timer
and its Elapsed event.
The problem is that I want the program to continue to run so that each
time the event is raised, the handler will be executed.
I tried using a while(timer.Enabled) {} loop but it made the process
use 90% cpu in the task manager. What is the correct way to wait for
an event and keep the program alive in a console application?
Thanks,
Aviv.




Just insert a System.Threading.Thread.Sleep ( 1000 ) into your loop.
The integer is milliseconds so adjust the number to suit your app...

presumably timer.Enabled is changed by the event handler? Otherwise you''ll
never leave the loop!
"avivgur" <av*****@gmail.com> wrote in message
news:2c*************************@posting.google.co m...

Hello,
I have devised a console application that uses a System.Timers.Timer
and its Elapsed event.
The problem is that I want the program to continue to run so that each
time the event is raised, the handler will be executed.
I tried using a while(timer.Enabled) {} loop but it made the process
use 90% cpu in the task manager. What is the correct way to wait for
an event and keep the program alive in a console application?
Thanks,
Aviv.



What I think he wants to do is just wait for a timer. How about
Thread.Sleep()?

Thomas P. Skinner [MVP]

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...

avivgur,

You need a way to block the current thread without performing
processing in that thread. What I would do is create an event, and wait
on the handle for that event. Then, I would have another thread wait on
the notification from the timer. When the notification comes in, then you
can set the event, and let the main thread continue processing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"avivgur" <av*****@gmail.com> wrote in message
news:2c*************************@posting.google.co m...

Hello,
I have devised a console application that uses a System.Timers.Timer
and its Elapsed event.
The problem is that I want the program to continue to run so that each
time the event is raised, the handler will be executed.
I tried using a while(timer.Enabled) {} loop but it made the process
use 90% cpu in the task manager. What is the correct way to wait for
an event and keep the program alive in a console application?
Thanks,
Aviv.




这篇关于在控制台应用程序中等待计时器事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆