在vb.net中留下时间延迟

发布于 2024-10-31 10:26:49 字数 352 浏览 0 评论 0原文

我有一个表单,在每行执行之前我需要给予 3 秒的时间延迟。但是当我尝试 system.threading.thread.sleep 时,它会冻结 UI,而且所有延迟都作为一个延迟执行。 也就是说,

如果我让

label.text = "intializing..."
sleep(5000)
label1.text = "connectinh..."
sleep(5000)
label2.text = "connected..."
sleep(5000)

UI 完全冻结 15 秒,然后所有执行都会立即发生,就好像它们之间没有延迟语句一样。除了 threading.sleep 之外我找不到其他任何东西。

I have a form in which I need to give timedelay of 3 seconds before each line executes. But when I try system.threading.thread.sleep it's freezing the UI and furthermore all delays are executing as one single delay.
that is

if I have

label.text = "intializing..."
sleep(5000)
label1.text = "connectinh..."
sleep(5000)
label2.text = "connected..."
sleep(5000)

the UI freezes for 15 seconds totally and then all executiong happens instantaneously as if there was no delay statement between them. I was not able to find out anything else other than threading.sleep.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兮子 2024-11-07 10:26:49

罗恩说的话:

Private Sub Form1_Shown(sender As Object, _
                        e As System.EventArgs) Handles Me.Shown
    Dim t As New Threading.Thread(AddressOf showLabelText)
    t.IsBackground = True
    t.Start()
End Sub

Const delaySecs As Integer = 3

Private Sub showLabelText()
    Dim del As New foo(AddressOf updLabel)

    Label1.BeginInvoke(del, "one")
    Threading.Thread.Sleep(delaySecs * 1000)

    Label1.BeginInvoke(del, "two")
    Threading.Thread.Sleep(delaySecs * 1000)

    Label1.BeginInvoke(del, "three")
    Threading.Thread.Sleep(delaySecs * 1000)

End Sub

Delegate Sub foo(theText As String)

Private Sub updLabel(theText As Object)
    Label1.Text = DirectCast(theText, String)
End Sub

What Ron said:

Private Sub Form1_Shown(sender As Object, _
                        e As System.EventArgs) Handles Me.Shown
    Dim t As New Threading.Thread(AddressOf showLabelText)
    t.IsBackground = True
    t.Start()
End Sub

Const delaySecs As Integer = 3

Private Sub showLabelText()
    Dim del As New foo(AddressOf updLabel)

    Label1.BeginInvoke(del, "one")
    Threading.Thread.Sleep(delaySecs * 1000)

    Label1.BeginInvoke(del, "two")
    Threading.Thread.Sleep(delaySecs * 1000)

    Label1.BeginInvoke(del, "three")
    Threading.Thread.Sleep(delaySecs * 1000)

End Sub

Delegate Sub foo(theText As String)

Private Sub updLabel(theText As Object)
    Label1.Text = DirectCast(theText, String)
End Sub
失去的东西太少 2024-11-07 10:26:49

内联解决方案,无多线程,无计时器。

    Sub delay(ByVal delay_ms As Integer)
        Dim tspan As New TimeSpan
        Dim tstart = Now
        While tspan.TotalMilliseconds < delay_ms
            tspan = Now - tstart
            Application.DoEvents()
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Hello"
        delay(100) 'leaks 572 ns every 10 ms
        Label1.Text = "Salut"
    End Sub

an inline solution, no multi-threading, no timers.

    Sub delay(ByVal delay_ms As Integer)
        Dim tspan As New TimeSpan
        Dim tstart = Now
        While tspan.TotalMilliseconds < delay_ms
            tspan = Now - tstart
            Application.DoEvents()
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Hello"
        delay(100) 'leaks 572 ns every 10 ms
        Label1.Text = "Salut"
    End Sub
顾北清歌寒 2024-11-07 10:26:49

您需要在单独的事件中更新每个标签,并让它刷新 UI。
创建一个计时器,它将调用您的事件处理程序并在其中每次更新不同的标签

You need to have each label update in a separate event and let it refresh the UI.
Create a timer that will call your event handler and in it update a different label every time

Bonjour°[大白 2024-11-07 10:26:49

请尝试以下操作:

label.text = "intializing..."
Application.DoEvents
sleep(5000)
label1.text = "connectinh..."
Application.DoEvents
sleep(5000)
label2.text = "connected..."
Application.DoEvents
sleep(5000)

Application.DoEvents 告诉操作系统进程底层工作,例如更新文本值。

Try the following:

label.text = "intializing..."
Application.DoEvents
sleep(5000)
label1.text = "connectinh..."
Application.DoEvents
sleep(5000)
label2.text = "connected..."
Application.DoEvents
sleep(5000)

Application.DoEvents tells the OS process underlying work, such as updating the text value.

尤怨 2024-11-07 10:26:49
label.text = "intializing..."
label.Refresh
sleep(5000)
label1.text = "connectinh..."
label1.Refresh
sleep(5000)
label2.text = "connected..."
label2.Refresh
sleep(5000)

我认为这应该比 DoEvents 工作得更正常!

label.text = "intializing..."
label.Refresh
sleep(5000)
label1.text = "connectinh..."
label1.Refresh
sleep(5000)
label2.text = "connected..."
label2.Refresh
sleep(5000)

I think this should work more properly than DoEvents!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文