如何在画完一条线后做出延迟

本文关键字:延迟 一条 | 更新日期: 2023-09-27 18:14:44

我在一个wpf c#应用程序上工作,我想在画一条线后,有1秒延迟而不阻塞UI。

我的绘图函数在这里:

    private void DrawLine(int serviceTime, string lineName)
    {
        while (serviceTime != 0)
        {
            (this[lineName] as ObservableCollection<Line>).Add(new Line { X1 = x1, Y1 = y1, X2 = x2, Y2 = y2 });
            //Delay Code
            x1 += 10;
            x2 += 10;
            serviceTime--;
        }
        ResetXY();
    }

如何在画完一条线后做出延迟

我使用Devexpress MVVM Dispatcher来解决我的问题:

    private void DrawLine(int serviceTime, string lineName)
    {
        while (serviceTime != 0)
        {
            DispatcherService.BeginInvoke(() =>
            {
                    (this[lineName] as ObservableCollection<Line>).Add(new Line { X1 = x1, Y1 = y1, X2 = x2, Y2 = y2 });
            });
            x1 += 10;
            x2 += 10;
            serviceTime--;
            Thread.Sleep(500);
        }
        ResetXY();
    }