简体   繁体   English

Thread.Sleep c#.NET

[英]Thread.Sleep c#.NET

I want to put a thread to sleep, and I don't have a sleep method.我想让一个线程进入睡眠状态,我没有睡眠方法。

I have using System.Threading .我有使用System.Threading

In my code i write:在我的代码中,我写道:

Thread t = new Thread(StartPointMethod);
t.

In the Methods list there is no Sleep....在方法列表中没有睡眠....

What could be the problem?可能是什么问题呢?

This code for sleep current thread for 20 second.此代码用于睡眠当前线程 20 秒。

System.Threading.Thread.Sleep(20000);

Use this method in any method in your new thread that you want to sleep.在您想要休眠的新线程中的任何方法中使用此方法。

System.Threading.Thread.Sleep(500);

Sleep is a static method on 'Thread', not an instance method.睡眠是“线程”上的 static 方法,而不是实例方法。 So the way to make you Thread sleep is to have a Thread.Sleep statement inside it executing method.所以让你线程睡眠的方法是在它执行方法中有一个 Thread.Sleep 语句。

Since Thread.Sleep will always make the executing thread sleep, you can do something in the line of the example below.由于 Thread.Sleep 将始终使正在执行的线程休眠,因此您可以在下面示例的行中执行一些操作。

    private void Foo()
    {
        Thread t = new Thread(new ThreadStart(ThreadWorker));
        t.Start();

        t.Join();
    }

    private void ThreadWorker()
    {
        Console.WriteLine("Prior to sleep");
        Thread.Sleep(100);
        Console.WriteLine("After sleep sleep");
    }

There's no way for thread A to tell thread B to sleep.线程 A 没有办法告诉线程 B 休眠。 That is, you can't write:也就是说,你不能写:

Thread t = new Thread(...);
t.Start();
t.Sleep();

You can suspend a thread, and then resume it later, but this is a very bad idea.可以暂停一个线程,然后稍后再恢复它,但这是一个非常糟糕的主意。 Doing so risks all kinds of potentially disastrous consequences.这样做可能会带来各种潜在的灾难性后果。 There's a reason that Thread.Suspend has been obsoleted. Thread.Suspend已经过时是有原因的。

In normal code (ie outside writing debuggers and OS-level stuff), there's never a good reason to suspend a thread.在普通代码中(即在编写调试器和操作系统级别的东西之外),没有充分的理由暂停线程。 And there's almost never a good reason to call Thread.Sleep .而且几乎没有一个很好的理由来调用Thread.Sleep If you find that you need to suspend or sleep a thread, there's almost certainly a design problem that you need to address.如果您发现需要暂停或休眠线程,则几乎可以肯定存在需要解决的设计问题。

you have to use this code:你必须使用这个代码:

Thread.Sleep(5);

Just to expand a little on the (correct) answers above, Thread.sleep is a static method.只是为了扩展上面的(正确)答案,Thread.sleep 是一种static方法。 Static methods are associated with a class (Thread), but not a particular instance of the class. Static 方法与 class(线程)相关联,但与 class 的特定实例无关。

So, to call Thread.Sleep - you just write (as has been said above) "Thread.Sleep(msecs)" - you don't need to create a thread to call the method.因此,要调用 Thread.Sleep - 您只需编写(如上所述)“Thread.Sleep(msecs)” - 您无需创建线程来调用该方法。

Thread.Sleep(milliseconds) lets the current thread sleep for x number of milliseconds. Thread.Sleep(milliseconds) 让当前线程休眠 x 毫秒。 There is no way for a thread to put another thread to sleep.一个线程没有办法让另一个线程进入睡眠状态。 Thread.Sleep always refers to current thread. Thread.Sleep 总是指当前线程。

The System.Thread has a function specifically made for that. System.Thread 有一个专门为此而设计的 function。

System.Threading.Thread.Sleep(100)

The Sleep function takes one argument in milliseconds. Sleep function 以毫秒为单位接受一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM