暂停子画面,同时允许用户操作其他子画面

时间:2019-03-01 12:14:41

标签: vb.net

您好,我正在尝试创建Windows Media Player类型的应用程序,特别是后退按钮。首先需要将歌曲重新设置为开始,如果在2秒钟内再次单击,则将歌曲恢复为之前的歌曲。

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

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

您应该使用Microsoft的Reactive Framework(又名Rx)-NuGet System.Reactive并添加using System.Reactive.Linq;-然后您可以执行以下操作:

var clicks =
    Observable
        .FromEventPattern(h => button.Click += h, h => button.Click -= h)
        .Select(ep => Unit.Default);

IObservable<string> query =
    clicks
        .Timestamp()
        .StartWith(Timestamped.Create(Unit.Default, DateTimeOffset.MinValue))
        .Publish(cs =>
            cs.Zip(cs.Skip(1), (c0, c1) =>
                c1.Timestamp.Subtract(c0.Timestamp).TotalSeconds >= 2.0
                    ? "Restart" 
                    : "Previous"));

IDisposable subscription =
    query
        .Subscribe(x =>
        {
            if (x == "Restart")
            {
                // Do Restart
            }
            else
            {
                // Do Previous
            }
        });

我知道这并不像做Await Task.Delay(2000)那样简单,但是Rx的力量是巨大的,值得学习。