📜  C#线程示例:线程Sleep()方法

📅  最后修改于: 2020-10-31 14:10:59             🧑  作者: Mango

C#线程示例:Sleep()方法

Sleep()方法将当前线程挂起指定的毫秒数。因此,其他线程有机会开始执行。

using System;
using System.Threading;
public class MyThread
{
    public void Thread1()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i);
            Thread.Sleep(200);
        }
    }
}
public class ThreadExample
{
    public static void Main()
    {
        MyThread mt = new MyThread();
        Thread t1 = new Thread(new ThreadStart(mt.Thread1));
        Thread t2 = new Thread(new ThreadStart(mt.Thread1));
        t1.Start();
        t2.Start();
    }
}

输出:

0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9