我怎么等到myVar == true [英] How do I wait until myVar == true

查看:50
本文介绍了我怎么等到myVar == true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家!


我如何做以下......


while(myVary!= true){};


显然我不想使用100%的处理器来保持这个

无限循环直到myVar == true。但是等我该怎么办?


谢谢你!

- 编码器;)

解决方案

也许更多的信息将按顺序排列 - 比如你想要做什么

做.....如果你只是想等待条件成真

- 您可以使用AutoResetEvent并使用WaitOne坐在它上面....如果你在
中正在循环中工作 - 它会更高效,更安全

(假设这是一些多线程代码的一部分)使用数字

参数和Interlocked.Read ...


John


Coder写道:

大家好!

我如何做以下......

while(myVary!= true){};

显然我不想使用100%的处理器来保持这个
无限循环,直到myVar == true。但是等我该怎么办?

谢谢你!
- 编码器;)



Coder< co ** ***@yahoo.com>写道:

各位大家!

我如何做以下......

while(myVary!= true){}; 无限循环,直到myVar == true。但等我该怎么做?




首先,我建议将条件写为!myVar。否则你

可能会输错,删除!并最终完成作业

你并不是故意的。


其次,你应该考虑线程问题。我假设

另一个线程是否会改变myVar的值?您需要确保

确保myVar是易失性的或使用包含

锁定的属性。请参阅
http://www.pobox。 com / ~siget / csharp / t ... latility.shtml 了解详情

了解为什么这是必要的。


好​​的,那么什么要做什么等待?你可以打电话给Thread.Sleep -

,但这意味着你不会像你想要的那样快速醒来,并且

你仍然需要每次循环检查它。


你真正想要的是手动/自动重置事件,或者使用

Monitor.Wait / Pulse - a说睡觉,直到我被告知醒来

这个对象。有关这些主题的更多信息,请参阅
http ://www.pobox.com/~skeet/csharp/t...eadlocks.shtml (中途)

http://www.pobox.com/~skeet/csharp/t...thandles.shtml


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件


无论你是否在主线程中这样做,我都会这样写:


while(!myVary){

System.Threading.Thread.Sleep(250); //暂停1/4秒;

};


这样你就可以在不占用资源的情况下将CPU交回操作系统。 />

Howdy everybody!

How do I do the following...

while (myVary != true){};

Obviously I do not want to use 100% of the processor to stay in this
infinite loop till myVar == true. But wait do I do?

Thanks yal!
- Coder ;)

解决方案

Perhaps more information would be in order -- like what are you trying
to do ..... if you are just trying to wait for the condition to be true
-- you can use an AutoResetEvent and sit on it with WaitOne .... if you
are doing work inside the loop -- it will be more efficient and safer
(assuming this is part of some multi-threaded code) to use a numeric
argument and Interlocked.Read ...

John

Coder wrote:

Howdy everybody!

How do I do the following...

while (myVary != true){};

Obviously I do not want to use 100% of the processor to stay in this
infinite loop till myVar == true. But wait do I do?

Thanks yal!
- Coder ;)



Coder <co*****@yahoo.com> wrote:

Howdy everybody!

How do I do the following...

while (myVary != true){};

Obviously I do not want to use 100% of the processor to stay in this
infinite loop till myVar == true. But wait do I do?



Firstly, I''d suggest writing the condition as !myVar. Otherwise you
might could make a typo, remove the "!" and end up with an assignment
you didn''t mean.

Secondly, you should consider the threading issues. I assume that
another thread is going to change the value of myVar? You need to make
sure that either myVar is volatile or use a property which includes
locking. See
http://www.pobox.com/~skeet/csharp/t...latility.shtml for details
on why this is necessary.

Okay, so what to do about waiting? You could just call Thread.Sleep -
but that means you won''t wake up as quickly as you might want to, and
you''ll still have to go round the loop checking it each time.

What you actually want is either a Manual/AutoResetEvent, or to use
Monitor.Wait/Pulse - a way of saying "Sleep until I''m told to wake up
by this object". For more information on those topics, see
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml (half way)
and http://www.pobox.com/~skeet/csharp/t...thandles.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Whether you do it in the main thread or not, I would write it like this:

while (!myVary) {
System.Threading.Thread.Sleep(250); // pause for 1/4 second;
};

That way you give the CPU back to the O/S without taking up resources.


这篇关于我怎么等到myVar == true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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