且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

同步等待,而不会阻塞UI线程

更新时间:2022-10-30 18:53:06

您可以使用DispatcherTimer$c$c>对于诸如此类的事情。

编辑:这可能会做的一样好......

 私人无效等待(双秒)
{
    VAR帧=新DispatcherFrame();
    新的Thread((的ThreadStart)(()=>
        {
            Thread.sleep代码(TimeSpan.FromSeconds(秒));
            frame.Continue = FALSE;
        }))。开始();
    Dispatcher.PushFrame(架);
}
 

(Dispatcher.PushFrame$c$c>文档。)

Is there a synchronous wait function that won't tie up the UI-thread in .NET WPF? Something like:

Sub OnClick(sender As Object, e As MouseEventArgs) Handles button1.Click
     Wait(2000) 
     'Ui still processes other events here
     MessageBox.Show("Is has been 2 seconds since you clicked the button!")
End Sub

You can use a DispatcherTimer for that sort of thing.

Edit: This might do as well...

private void Wait(double seconds)
{
    var frame = new DispatcherFrame();
    new Thread((ThreadStart)(() =>
        {
            Thread.Sleep(TimeSpan.FromSeconds(seconds));
            frame.Continue = false;
        })).Start();
    Dispatcher.PushFrame(frame);
}

(Dispatcher.PushFrame documentation.)