Thread.sleep代码 [英] Thread.Sleep

查看:96
本文介绍了Thread.sleep代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你想继续阅读物品的价值(10秒)

不断变化,

你会怎么做?我试过这个,但是没用了

线程th =新线程(新的ThreadStart(自述文件));

th.Start();

Thread.Sleep(3000);

th.Abort();

Thread.Sleep(1000);

If you want to keep reading the value of a property (10 seconds) that
keeps changing,
how would you do it. I tried this, but it did not work
Thread th = new Thread(new ThreadStart(ReadMe));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

推荐答案

JPSutor,


我不会使用像这样的轮询机制。相反,我会在课堂上有一些

类型的通知,指示属性何时发生变化。

框架有这样的标准。


第一个(.NET 2.0之前的版本)是在您的班级上有一个名为

< property name>已更改的事件。此事件的类型为EventHandler,当事件发生更改时,您将触发它。


如果您使用的是.NET 2.0,则应使用INotifyPropertyChanged

界面指示属性何时发生变化(

一般比旧方法更有效)。


如果你想要调查属性,然后我会使用一个计时器,而不是

导致线程进入睡眠状态,并检查那里的属性值。


希望这有帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" JPSutor" < JP ***** @ yahoo.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
JPSutor,

I wouldn''t use a polling mechanism like this. Rather, I would have some
sort of notification on class which indicates when the property changes.
The framework has a standard for things like this.

The first (pre .NET 2.0) is to have an event on your class named
<property name>Changed. This event is of type EventHandler, and you fire it
when the event changes.

If you are using .NET 2.0, you should use the INotifyPropertyChanged
interface to indicate when a property changed (it''s more efficient in
general than the old method).

If you want to poll the property, then I would use a timer, instead of
causing a thread to sleep, and check the property value there.

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

"JPSutor" <jp*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
如果你想继续阅读
不断变化的房产价值(10秒),你会怎么做?我试过这个,但是没用了

线程th =新线程(新的ThreadStart(自述文件));
th.Start();
Thread.Sleep(3000) ;
th.Abort();
Thread.Sleep(1000);
If you want to keep reading the value of a property (10 seconds) that
keeps changing,
how would you do it. I tried this, but it did not work
Thread th = new Thread(new ThreadStart(ReadMe));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);





我也可能会添加一个具有睡眠持续时间的while循环非常好地工作。我注意到在.Net 2.0之前,计时器在某些情况下不够可靠

。一个while循环似乎可以作为替代

解决方案。

Nicholas Paldino [.NET / C#MVP]写道:

I might also add that a while loop with a sleep duration works quite
well. I noticed in pre .Net 2.0 the timer just wasn''t reliable enough
in certain situations. A while loop seemed to work well as an alternate
solution.
Nicholas Paldino [.NET/C# MVP] wrote:
JPSutor,

我不会使用像这样的轮询机制。相反,我会在类上有一些
类型的通知,指示属性何时发生变化。
框架有这样的标准。

第一个(.NET之前) 2.0)在你的班级上有一个名为
< property name> Changed的事件。此事件的类型为EventHandler,并在事件发生变化时触发它。

如果您使用的是.NET 2.0,则应使用INotifyPropertyChanged
接口指示何时属性改变了(它比一般方法更有效率。)

如果你想轮询属性,那么我会用一个计时器,而不是
导致线程进入睡眠状态,并检查其中的属性值。

希望这会有所帮助。
JPSutor,

I wouldn''t use a polling mechanism like this. Rather, I would have some
sort of notification on class which indicates when the property changes.
The framework has a standard for things like this.

The first (pre .NET 2.0) is to have an event on your class named
<property name>Changed. This event is of type EventHandler, and you fire it
when the event changes.

If you are using .NET 2.0, you should use the INotifyPropertyChanged
interface to indicate when a property changed (it''s more efficient in
general than the old method).

If you want to poll the property, then I would use a timer, instead of
causing a thread to sleep, and check the property value there.

Hope this helps.





线程th =新线程(新的ThreadStart(自述文件));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

Thread th = new Thread(new ThreadStart(ReadMe));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);



这不起作用导致Thread.Sleep引用当前线程,线程,为此工作Thread.Sleep在ReadMe()方法中应该是



无需调用Abort顺便说一句。

ITOH,我不会这样做就这样,我会用一个计时器。


甚至更好的活动。



-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


This does not work cause Thread.Sleep refer to the current thread , the
thread where the method is called, for this to work Thread.Sleep should be
in the ReadMe() method.
No need to call Abort btw.
ITOH, I would not do it this way, I would use a timer .

Or even better an event.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


这篇关于Thread.sleep代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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