关于一段时间(真实)循环的问题 [英] A question about a while (true) loop

查看:61
本文介绍了关于一段时间(真实)循环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对所有人来说,


我用C#和.Net

框架3.0编写了一个SerialPort类/应用程序。

但我有一个问题;因为我的serialPort类/应用程序不是一个Windows窗体应用程序(而只是一个控制台程序);

因此我需要一种方法使应用程序活着。直到我杀死申请或输入退出通过串口发出信号。


我尝试过最简单的方法让应用程序活着使用

" while(true)"环;但当然,结果并不好,因为

" while(true)"循环将消耗100%的CPU处理并减慢

PC。


那么还有其他方法可以让控制台应用程序活着吗?而不是
消耗cpu 100%?我在想,如果一个线程可能会有所帮助,但是我不知道怎么样......我还在想,必须有一些技巧

来创建一个应用程序活着"并且不会像Windows一样消耗cpu时间

表单(即,当有任何

用户事件或系统事件时它只消耗cpu处理能力)


任何帮助将不胜感激:-)


来自Jason(Kusanagihk)

To all,

I have written a SerialPort class / application using C# and .Net
Framework 3.0.
but I have a question; since my serialPort class / application is not
a Windows Form application (rather it is just a console program);
therefore I need a way to make the application "alive" until I kill
the application or enter a "quit" signal through the serial port.

I have tried the simplest way to make the app "alive" by using a
"while (true)" loop; but of course, the result is not good since a
"while(true)" loop will consume 100% cpu processing and slow down the
PC too.

So are there any other ways to keep a console app "alive" and not
consuming the cpu 100%? I am thinking if a Thread might help, but I
have no idea how... Also I was thinking, there must be some techniques
to make an app "alive" and won''t consume cpu time just like a Windows
Form did (ie. it only consumes cpu processing power when there are any
user events or system events)

Any help would be appreciated :-)

From Jason (Kusanagihk)

推荐答案

嗨Jason,


你应该使用同步对象和函数

像WaitForSingleObject来阻止你的应用程序执行

而不影响整个操作系统。看这里:


[WaitForSingleObject(kernel32)]
http://pinvoke.net/default.aspx/kern...gleObject.html

来一点但很好的例子如何运作!


MSDN [WaitForSingleObject]
http://msdn2.microsoft.com/en-us/library/ms687032.aspx

查看同步化及其功能的所有信息
以及这些东西如何运作以获取更多详细信息:


问候


Kerem


-

-----------------------

BesteGrüsse/祝你好运/ Votre bien devoue

KeremGümrükcü

Microsoft Live Space: http://kerem-g.spaces.live.com/

最新O笔源项目: http://entwicklung.junetz.de

-----------------------

此回复按原样提供,不附带任何明示或暗示保证。
Hi Jason,

you should work with synchronozation objects and functions
like WaitForSingleObject to block your applications execution
without affecting the overall operating system. See here:

[WaitForSingleObject (kernel32)]
http://pinvoke.net/default.aspx/kern...gleObject.html
Comes woth a little but good example how this works!

MSDN[WaitForSingleObject]
http://msdn2.microsoft.com/en-us/library/ms687032.aspx
See all the information for syncronization and its functions
and how this stuff works for more and detailed information:

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."


Jason,


我假设您还没有编写新的SerialPort类,而是

使用System.IO.Ports命名空间中的类。


您是否需要在此过程中收集输入,或者是您的应用程序

严格显示输出?如果它是严格显示输出,那么也许你应该从基于事件的模型转到拉模型,在那里你查询

端口以获取你需要的数据,并输出它。这将使您的应用程序保持活力。


如果您希望继续使用基于事件的模型,那么您可以随时使用

只需执行循环Console.ReadLine在里面。您可以忽略

输入,或者在适当时处理它,当您确定

时间正确时退出循环。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam。 guard.caspershouse.com


" Jason(Kusanagihk)" < ku ******** @ gmail.com写信息

新闻:11 ********************** ************ @ b40g2000 prf.googlegroups.com ...
Jason,

I''m assuming you haven''t written a new SerialPort class, but rather, are
using the class in the System.IO.Ports namespace.

Do you need to gather input during this process, or is your application
strictly showing output? If it is strictly showing output, then perhaps you
should go from an event-based model to a pull model, where you query the
port for the data you need, and output it. This will keep your app alive.

If you wish to stay with the event-based model, then you could always
just stick perform a loop with a Console.ReadLine in it. You could ignore
the input, or process it as appropriate, exiting the loop when you determine
the time is right.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jason (Kusanagihk)" <ku********@gmail.comwrote in message
news:11**********************************@b40g2000 prf.googlegroups.com...

对所有人来说,


我用C#和.Net

Framework 3.0编写了一个SerialPort类/应用程序。

但我有一个问题;因为我的serialPort类/应用程序不是一个Windows窗体应用程序(而只是一个控制台程序);

因此我需要一种方法使应用程序活着。直到我杀死申请或输入退出通过串口发出信号。


我尝试过最简单的方法让应用程序活着使用

" while(true)"环;但当然,结果并不好,因为

" while(true)"循环将消耗100%的CPU处理并减慢

PC。


那么还有其他方法可以让控制台应用程序活着吗?而不是
消耗cpu 100%?我在想,如果一个线程可能会有所帮助,但是我不知道怎么样......我还在想,必须有一些技巧

来创建一个应用程序活着"并且不会像Windows一样消耗cpu时间

表单(即,当有任何

用户事件或系统事件时它只消耗cpu处理能力)


任何帮助将不胜感激:-)


来自Jason(Kusanagihk)
To all,

I have written a SerialPort class / application using C# and .Net
Framework 3.0.
but I have a question; since my serialPort class / application is not
a Windows Form application (rather it is just a console program);
therefore I need a way to make the application "alive" until I kill
the application or enter a "quit" signal through the serial port.

I have tried the simplest way to make the app "alive" by using a
"while (true)" loop; but of course, the result is not good since a
"while(true)" loop will consume 100% cpu processing and slow down the
PC too.

So are there any other ways to keep a console app "alive" and not
consuming the cpu 100%? I am thinking if a Thread might help, but I
have no idea how... Also I was thinking, there must be some techniques
to make an app "alive" and won''t consume cpu time just like a Windows
Form did (ie. it only consumes cpu processing power when there are any
user events or system events)

Any help would be appreciated :-)

From Jason (Kusanagihk)




" Jason(Kusanagihk)" < ku ******** @ gmail.com写信息

新闻:11 ********************** ************ @ b40g2000 prf.googlegroups.com ...

"Jason (Kusanagihk)" <ku********@gmail.comwrote in message
news:11**********************************@b40g2000 prf.googlegroups.com...

对所有人来说,


我用C#和.Net

Framework 3.0编写了一个SerialPort类/应用程序。

但我有一个问题;因为我的serialPort类/应用程序不是一个Windows窗体应用程序(而只是一个控制台程序);

因此我需要一种方法使应用程序活着。直到我杀死申请或输入退出通过串口发出信号。


我尝试过最简单的方法让应用程序活着使用

" while(true)"环;但当然,结果并不好,因为

" while(true)"循环将消耗100%的CPU处理并减慢

PC。


那么还有其他方法可以让控制台应用程序活着吗?而不是
消耗cpu 100%?我在想,如果一个线程可能会有所帮助,但是我不知道怎么样......我还在想,必须有一些技巧

来创建一个应用程序活着"并且不会像Windows一样消耗cpu时间

表单(即,当有任何

用户事件或系统事件时它只消耗cpu处理能力)


任何帮助将不胜感激:-)
To all,

I have written a SerialPort class / application using C# and .Net
Framework 3.0.
but I have a question; since my serialPort class / application is not
a Windows Form application (rather it is just a console program);
therefore I need a way to make the application "alive" until I kill
the application or enter a "quit" signal through the serial port.

I have tried the simplest way to make the app "alive" by using a
"while (true)" loop; but of course, the result is not good since a
"while(true)" loop will consume 100% cpu processing and slow down the
PC too.

So are there any other ways to keep a console app "alive" and not
consuming the cpu 100%? I am thinking if a Thread might help, but I
have no idea how... Also I was thinking, there must be some techniques
to make an app "alive" and won''t consume cpu time just like a Windows
Form did (ie. it only consumes cpu processing power when there are any
user events or system events)

Any help would be appreciated :-)



为什么你不能通过使用

Thread.Sleep(等待时间)或使用带有计时器经过时间事件的计时器?

Why can''t you put the application to sleep by using the
Thread.Sleep(waittime) or using a Timer with an Timer Elapsed time event?


这篇关于关于一段时间(真实)循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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