C#怎样实现延时执行代码的功能

2025-04-14 06:07:45
推荐回答(4个)
回答1:

最笨的方法空循环

for(int i=0;i<=100;i++)
{
for(int j=0;j<=30000;j++) ;
}

回答2:

Thread.Sleep(100);

回答3:

Thread.Sleep()延迟
或多线程class Test
{
public static Int64 i = 0;
public static void Add()
{
for (int i = 0; i < 100000000; i++)
{
Interlocked.Increment(ref Test.i);
}
}
public static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(Test.Add));
Thread t2 = new Thread(new ThreadStart(Test.Add));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(Test.i.ToString());
Console.Read();
}

回答4:

Thread.Sleep