循环每3秒循环执行一些代码并退出循环 [英] Loop Execute Some Code Every 3 Second and Exit Loop

查看:198
本文介绍了循环每3秒循环执行一些代码并退出循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject我需要一些帮助,如何让循环每3秒执行一些代码?

以及如何退出循环?我需要一些建议我仍然是vb.net的初学者。

谢谢。



到目前为止我的代码:



Hello Codeproject i need some help how to make loop execute some code every 3 second?
and how to exit the loop? i need some suggestion i am still beginner in vb.net.
thank you.

this my code so far :

Sub Loops(ByVal key As String)
'some code to make delay 3 second
'and then execute below code
  SendKeys.Send(key)
End Sub





所以如果我这样叫这个代码



so if i am call this code like this

loop("B")



它将每3秒生成一次自动发送键B.

我需要一些代码如何退出循环?



谢谢b efore。


it will make automatic sendkeys B every 3 second.
and i need some code how to exit the loop?

thank you before.

推荐答案

大多数语言变体都有sleep()或wait()类型方法。 VB.Net做 - 睡眠(n),其中n是毫秒数 - 所以3000毫秒相当于你的3秒



你需要导入System.Threading和使用Sleep(3000)或使用Thread.Sleep(3000)



我要指出'睡眠'这样的大问题是它会阻塞线程(即没有别的事情会发生,没有UI更新等)..



'g'
Most language variants have a sleep() or wait() type method. VB.Net does - Sleep(n) where n is the number of milliseconds - so 3000 milliseconds is equivalent to your 3 secs

you need to import System.Threading and use Sleep(3000) or use Thread.Sleep(3000)

I'd point out that the big issue with 'Sleep' as such is it blocks the thread (ie nothing else will happen, no UI updates etc) ..

'g'


好的,我想我误会了你的关于你要做什么的问题..你在'循环'方法的原始上下文中提出问题'如何每3秒执行一次代码'这是一个好的开始

<如果你有修改后的代码,在RedemptionCheckBox124中,'循环'将被执行10次 - 这是你设置的循环..这10次中的每一次都要等待3000毫秒/ 3秒 - 你没有停止'循环'这样,它停止/控制返回RedemptionCheckBox124后整整10循环3秒.....我怀疑你想要的是像背景工作者那样,但我认为它远高于你的经验水平



http://www.dotnetperls.com/backgroundworker-vbnet [ ^ ]



...我不确定标准的VB.Net BackGroundWorker可以提前取消/终止
ok, I think Ive misunderstood your question on what you're trying to do ..as you asked the question, 'how to execute code every 3 seconds' within the original context of your 'Loops' method it was an ok start

as you have the modified code, in RedemptionCheckBox124, 'Loops' will be executed 10 times - this is a loop you set up .. every one of those 10 times its going to wait 3000 milli/3 seconds - you dont stop 'Loops' as such, it stops/control returns to RedemptionCheckBox124 after the entire 10 loops of 3 seconds ..... what I suspect you are trying to get to is something like a Background worker, but I think it's way above your experience level

http://www.dotnetperls.com/backgroundworker-vbnet[^]

...Im not sure if the standard VB.Net BackGroundWorker can be cancelled/terminated early though


这篇关于循环每3秒循环执行一些代码并退出循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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