0

我在 WPF 中有 C#.net 应用程序,其中我们有很多昂贵的操作。最近我们有这个要求:对于长时间运行的进程,显示忙碌光标直到 3 秒,如果操作超过 3 秒,则显示启动屏幕,直到操作完成。我在网上搜索了很多,但似乎没有什么相关的。任何支持将不胜感激。

我们已经尝试了一些东西,它有效,但在少数情况下它没有给出预期的结果。任何帮助将不胜感激。当我的内部逻辑显示任何警报消息并等待用户输入时,忙碌光标功能计时器继续运行,我们也得到了启动。

    public class BusyCursor:IDisposable
    {
        private Cursor _previousCursor;
        System.Timers.Timer _timer     
        public BusyCursor()
        {
            _previousCursor = Mouse.OverrideCursor;
            Mouse.OverrideCursor = Cursors.Wait;
           _timer = new System.Timers.Timer(); 
           _timer.Interval = 3000;
           _timer.Elapsed += timer_Tick;
           _timer.Start();
        }
        public void timer_Tick(object sender, ElapsedEventArgs e)
        {           
            Application.Current.Dispatcher.Invoke((new Action(() =>
                {
                    if (!DXSplashScreen.IsActive)
                    {
                        DXSplashScreen.Show<TrippsSplashScreen>();
                    }
                    Mouse.OverrideCursor = Cursors.Arrow;
                    _timer.Stop();

                })), DispatcherPriority.ApplicationIdle);            

        }
        #region IDisposable Members
        public void Dispose()
        {            
                _timer.Stop();
                if (DXSplashScreen.IsActive)
                {
                    DXSplashScreen.Close();
                }
                Mouse.OverrideCursor = Cursors.Arrow;               
        }
        #endregion
    }

Usage:

using (new BusyCursor())
{
       //logic ---
}

谢谢

4

1 回答 1

0
bool calculating = false;
bool showingSplash = false;
void Meth(Task[] expensiveCalls)
{
    Task.Factory.StartNew(() =>
    {
        calculating = true;
        Task.Factory.StartNew(() =>
        {
            Task.Delay(3000).Wait();
            if (calculating)
            {
                showingSplash = true;
                //Application.Current.Dispatcher.Invoke(() => show_sphlash());
            }
        });
        Task.WaitAll(expensiveCalls);
        calculating = false;
        if (showingSplash)
        {
            //Application.Current.Dispatcher.Invoke(() => hide_sphlash());
            showingSplash = false;
        }
    }
    );
}
于 2018-02-20T12:02:55.170 回答