gpt4 book ai didi

vb.net - 如何在visual basic 2008中为下一个循环使用计时器

转载 作者:行者123 更新时间:2023-12-04 18:16:43 25 4
gpt4 key购买 nike

我有一个案例,我需要生成数百万个唯一代码。为此,我创建了一个生成函数,其中生成随机数。我从 for 循环调用此函数并将生成的数字添加到列表框中。我的代码如下

  for i=1 to val(txtnumber.txt)
mynum=generate()
next

我在表单上创建了一个标签,我想在其中显示处理循环时经过的秒数。我使用定时器控制作为
     timer1.start()
for i=1 to val(txtnumber.text)
mynum=generate()
listbox1.items.add(mynum)
next
timer1.stop

和 timer1_tick 功能
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Val(Label1.Text) + 1
End Sub

但是当我单击生成按钮时,会生成所有数字,但计时器不显示耗时。

我可能错过了一些东西,所以请帮帮我

最佳答案

这可能最好在 BackgroundWorker 中处理。在表格上放置一个并设置其WorkerReportsProgress=True .此外,在 ListBox 中放置一百万个数字可能不是一个好主意,所以我省略了这一点。

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim started As DateTime = Now
For i As Integer = 1 To val(txtnumber.txt)
mynum=generate()
BackgroundWorker1.ReportProgress(i, Nothing)
Next
Dim ended As TimeSpan = Now.Subtract(started)
BackgroundWorker1.ReportProgress(0, ended.TotalSeconds.ToString)
End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If e.UserState IsNot Nothing Then
Label1.Text = e.UserState.ToString()
Else
Label1.Text = e.ProgressPercentage.ToString
End If
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Button1.Enabled = True
End Sub

当工作人员报告 ProgressChanged 时,您的标签应该会正确更新。事件。

关于vb.net - 如何在visual basic 2008中为下一个循环使用计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com