vb.net X秒/点击后如何停止计时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15727666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop timer after X seconds/clicks
提问by Hyman C.
I have an application that automatically clicks on a button for me about 1 time every second depending on the speed I choose. However, it times out so I need to add a refresh to this page. I need to stop my timer1after so many clicks and have a WebBrowser1.Refresh(), then once the refresh is done, timer1kicks in with the clicks and the process is done all over again. I have no idea how to start this because I haven't ever had to stop a timer before, but I've been reading up on it and it looks relatively easy. Here is the script I'm working with:
我有一个应用程序,根据我选择的速度,它每秒自动为我点击一次按钮。但是,它超时了,所以我需要刷新此页面。timer1在点击了这么多次之后,我需要停止我的操作并有一个WebBrowser1.Refresh(),然后一旦刷新完成,就timer1开始点击,然后整个过程重新完成。我不知道如何开始这个,因为我以前从来没有停止过计时器,但我一直在阅读它,它看起来相对容易。这是我正在使用的脚本:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Timer1.Interval = 40
ElseIf RadioButton2.Checked = True Then
Timer1.Interval = 100
Else
Timer1.Interval = 500
End If
If ((WebBrowser1.IsBusy)) Then
Else
WebBrowser1.Document.GetElementById("NewGamertag").SetAttribute("value", txtTurbo.Text)
Timer1.Start()
End If
End Sub
As you can see, once the button is clicked once it does Timer1.Start()over and over again, and in Timer1.start()'s code you can see the code to click once:
正如你所看到的,一旦按钮被点击一次,它就会Timer1.Start()一遍又一遍地重复,在Timer1.start()的代码中你可以看到点击一次的代码:
WebBrowser1.Document.GetElementById("claimIt").InvokeMember("Click")
Which it works, but I need to stop my Timer1after so many clicks/seconds and have a refresh web browser in. I have no idea how to start this, and if anyone could lead me in the right tracks I'd be thankful. (Sorry for bad grammar/run on sentences, I'm new to English)
它有效,但我需要Timer1在这么多次点击/秒后停止我的浏览器并刷新网络浏览器。我不知道如何启动它,如果有人能引导我走上正确的轨道,我将不胜感激。(对不起,语法错误/在句子上运行,我是英语新手)
回答by dotNET
There are several problems with your code. First of all you don't appear to be using Timer_Tickor Timer_Elapsedevent anywhere (I don't know which Timer are you using; there are multiple timer classes in .NET Framework), which is the ACTUAL event that would be fired upon interval completion. Secondly, Timer can be enabled/disabled simply by settings its Enabledproperty, which you're not doing anywhere. You should also take a look at how Enabledproperty works in conjunction with Intervalproperty to make sure it behaves exactly as you expect.
您的代码有几个问题。首先,您似乎没有在任何地方使用Timer_Tick或Timer_Elapsed事件(我不知道您使用的是哪个 Timer;.NET Framework 中有多个计时器类),这是在间隔完成时触发的 ACTUAL 事件。其次,只需通过设置其Enabled属性即可启用/禁用 Timer ,而您在任何地方都不会这样做。您还应该查看Enabled属性如何与Interval属性结合使用,以确保它的行为完全符合您的预期。
回答by Alex
The easiest, but not necessarily the "cleanest" way is to have a public integer variable declared.
最简单但不一定是“最干净”的方法是声明一个公共整数变量。
I.e.
IE
Public Class Form1
'Public variables are decleared here
Dim number_of_ticks as integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
On the Timer_Tick event, you simply add 1 to the number_of_ticks variable.
在 Timer_Tick 事件中,您只需将 number_of_ticks 变量加 1。
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If Not number_of_ticks > 100 Then
'[EDIT:] I forgot to increase the number_of_ticks
number_of_ticks += 1
'put your code here
Else
'number_of_ticks has exceed the maximum amount of allowed ticks
Timer1.Stop()
End If
End Sub
[EDIT:] The number_of_ticks += 1 increases the variable once every time the code is run. Once again, I apologize for my mistake.
[编辑:] number_of_ticks += 1 每次运行代码时都会增加一次变量。再次为我的错误道歉。
To limit the amount of ticks allowed, simply increase / decrease the number_of_ticks variable. If your timer interval is 500ms then the Timer_Tick event will be raised two times a second, therefore after one second - the number_of_ticks will be 2. If you want the timer to run for 10 seconds, set the number_of_ticks to 20.
要限制允许的滴答数,只需增加/减少 number_of_ticks 变量。如果您的计时器间隔为 500 毫秒,则 Timer_Tick 事件将每秒引发两次,因此在一秒后 - number_of_ticks 将为 2。如果您希望计时器运行 10 秒,请将 number_of_ticks 设置为 20。
If this doesn't help, please question my answer.
如果这没有帮助,请质疑我的回答。
Good Luck!
祝你好运!

