暂停一个子程序,同时允许用户操作不同的子程序 [英] Pause a sub while allowing user to operate a different sub

查看:20
本文介绍了暂停一个子程序,同时允许用户操作不同的子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 windows 媒体播放器类型的应用程序,特别是后退按钮.需要在第一次点击时将歌曲设置回开头,如果在 2 秒内再次点击,则将歌曲恢复到之前的歌曲.

Hi im trying to create a windows media player type application, specifically the back button. It needs to on the first click set the song back to the start, and if clicked again within 2 seconds change the song back to the one before it.

为此,我将 2 个单独的按钮与 2 个单独的程序重叠,我希望第一个按钮在点击后隐藏 2 秒,然后在 2 秒后显示.虽然 iv 找到了暂时挂起程序的方法,但它不允许我单击后面的按钮来激活其功能.这是我想出的测试代码,任何帮助表示赞赏-

To do this i have overlapped 2 seperate buttons with 2 seperate procedures, i want the first button to hide for 2 seconds once clicked, and then show after those 2 seconds. ALthough iv found ways to temporarily suspend the program, it doesnt allow me to click to button behind to activate its features. This is the test code i came up with, any help appreciated-

Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
    PictureBox5.Visible = False
    System.Threading.Thread.Sleep(2000)
    PictureBox5.Visible = True
End Sub

Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
    MsgBox("Success")
End Sub

推荐答案

你只需要一点点async/await 魔法.通过这种组合,下方的按钮将在两秒内响应:

You just need a little async/await magic. With this combination, the button underneath will be responsive during the two second period:

Private Async Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
    PictureBox5.Visible = False
    Await Task.Delay(2000)
    PictureBox5.Visible = True
End Sub

注意事件处理程序签名中Private之后的Async

Notice the Async after Private in the event handler signature!

这篇关于暂停一个子程序,同时允许用户操作不同的子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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