如何使用计时器暂停执行C#代码 [英] How to pause execution of C# code using timer

查看:114
本文介绍了如何使用计时器暂停执行C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要停止执行代码一段时间。我使用了thread.sleep()但是在循环中它没有正常运行并立即执行下一行代码。

例如:



  //  如果这是第一行 
string abc = 这是第一行;

// 之后我想暂停执行应用程序5秒钟
// 然后想在下面执行。

MessageBox.Show(abc);





请告诉我thread.sleep()的任何替代方法。

解决方案

你可以 - 这并不困难:

睡眠( 5000 ); 

会这样做。



但是......它会锁定你的用户界面,直到时间过去。

如果你当时想要做任何其他事情(或者你不喜欢你的用户认为你的应用已经崩溃)那么我会考虑使用Timer,甚至是BackgroundWorker来完成长期任务


如果查看intellisense或在MSDN上,Thread.Sleep()接受一个参数,该参数是睡眠时间的值以毫秒为单位的线程。



要让代码在显示消息框之前等待5秒,请尝试:



 Thread.Sleep( 5000 ); 
MessageBox.Show(abc);







你需要包括:





名称空间,否则使用该方法的完全限定路径:



 System.Threading.Thread.Sleep(5000); 





好​​吧,如果由于某些奇怪的原因你的thread.sleep()不能正常工作(我真的想把它弄清楚,因为这意味着它的存在其他事情发生 - 可能你有另一个线程重新进入某种方法)你总是可以尝试这个:



  var  waitTime =  new  TimeSpan( 0  0  5 ); 
var waitUntil = DateTime.Now + waitTime;

while (DateTime.Now < = waitUntil)
{
System.Threading.Thread.Sleep( 1000 );
//
/ /
//
}

MessageBox.Show(abc);





如果是这样的话仍然无效,您在代码或系统中都有其他问题。


Thread.Sleep [ ^ ]应该可以正常工作。



请看下面的代码开头:



  string  message =   Hello world!; 

for int count = 0 ; count < 10 ; count ++)
{
Console.WriteLine( string .Format( {0 }:{1},count,message));

// 等待2秒
Thread.Sleep( 2000 );
}


Hi all,
I need to stop the execution of the code for some while. I used thread.sleep() but in the loop it's not behaving correctly and instantly executing next lines of code.
For example:

// If this is the first line 
string abc = "This is first line";

// and after that I want to pause the execution of the application for 5 seconds and 
// then want to execute below line.

MessageBox.Show(abc);



Please tell me any alternative for the thread.sleep().

解决方案

You can - it's not difficult:

Sleep(5000);

Will do it.

But...it'll lock up your user interface until the time has passed.
If you want to do anything else at the time (or you don't like your user thinking your app has crashed) then I'd consider using a Timer, or even a BackgroundWorker for the long task


If you look at the intellisense or on MSDN, Thread.Sleep() takes an argument that is the value of time to "sleep" the thread in milliseconds.

To have your code wait 5 seconds before showing the messagebox, try:

Thread.Sleep(5000);
MessageBox.Show(abc);




You will need to include:

using System.Threading;



in your namespaces, otherwise use the fully qualified path to the method:

System.Threading.Thread.Sleep(5000);



Well, if for some bizarre reason your thread.sleep() isn't working (I'd REALLY want to figure this one out because it means there's something else happening - likely you have another thread re-entering some method) you can always try this:

var waitTime = new TimeSpan(0, 0, 5);
var waitUntil = DateTime.Now + waitTime;

while(DateTime.Now <= waitUntil)
{
    System.Threading.Thread.Sleep(1000);
    // .
    // .
    // .
}

MessageBox.Show(abc);



If that still doesn't work, you have OTHER problems either in code or within your system.


Thread.Sleep[^] should work just fine.

Look at the code below to start with:

string message = "Hello world!";

for (int count = 0; count < 10; count++)
{
    Console.WriteLine(string.Format("{0}: {1}", count, message));

    //wait for 2 seconds
    Thread.Sleep(2000);
}


这篇关于如何使用计时器暂停执行C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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