如何显示忙碌光标x次,然后显示闪屏?

时间:2018-02-20 09:11:55

标签: c# wpf splash-screen busy-cursor

我在使用C#.net的WPF中有应用程序,其中我们有很多昂贵的操作。最近我们得到了这个要求:对于长时间运行的进程,将忙碌光标显示为3秒,如果操作超过3秒,则显示闪屏,直到操作完成。我在网上搜索了很多但似乎没什么关系。任何支持都将受到高度赞赏。

我们已经尝试了一些东西,它有效,但在少数情况下它没有给出预期的结果。任何帮助将受到高度赞赏。 当我的内部逻辑显示任何警告消息并等待用户输入时,Busy Cursor函数计时器继续运行,我们也得到了启发。

    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 ---
}

谢谢

1 个答案:

答案 0 :(得分: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;
        }
    }
    );
}
相关问题