如何添加的每个迭代之间有一些延迟在C#中循环? [英] How to add some delay between each iteration of for loop in C#?

查看:506
本文介绍了如何添加的每个迭代之间有一些延迟在C#中循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的ASP.NET MVC3用C#。
我想补充的循环的每个迭代之间有一些延迟。

I'm working on ASP.NET MVC3 with C#. I want to add some delay between each iteration of for loop.

for(int i=0; i<5; i++)
{
    //some code
    //add delay here
}

所以,我怎么能这样做?

So How can I do this?

推荐答案

至于其他的答案已经表示,采用<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep.aspx\"><$c$c>Thread.Sleep将一个线程睡眠...但考虑到你在写一个web应用程序,我不认为它会做你想让它是什么。

As other answers have said, using Thread.Sleep will make a thread sleep... but given that you're writing a web app, I don't think it'll do what you want it to.

如果目的是为的用户的看东西在同一时间发生的一秒,你就需要把延迟在客户端。如果您有五个一秒钟的延迟,你发送一个HTTP响应之前,这只是意味着用户将不得不等待五秒钟,他们看到你的结果页面之前。

If the aim is for users to see things happening one second at a time, you'll need to put the delay on the client side. If you have five one-second delays before you send an HTTP response, that just means the user will have to wait five seconds before they see your result page.

这听起来像你应该使用AJAX来这样做。

It sounds like you should be doing this with AJAX.

编辑:正如在评论中指出,这可能是值得你在看<一个href=\"https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout\"><$c$c>window.setTimeout javascript调用。 (我不做的Javascript,所以我怕我不能帮助任何周围的细节。)

As noted in comments, it's probably worth you looking at the window.setTimeout Javascript call. (I don't "do" Javascript, so I'm afraid I won't be able to help with any of the details around that.)

这篇关于如何添加的每个迭代之间有一些延迟在C#中循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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