0

I have application in WPF with C#.net in which we have lot of expensive operations. Recently we got this requirement: For long running processes display the busy cursor till 3 seconds, if operation is exceeding 3 second display the splash screen until operation is finished. I have searched a lot over the net but nothing seems to be relevant. Any support will be high appreciated.

We have tried something, it works but in few case it is not giving expected results. Any help would be highly appreciated. When my internal logic display any alert message and wait for the user input, Busy Cursor function timer keep running, we got the splash too.

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

Thanks

Rahim Ali
  • 11
  • 1
  • 1
    There are several *thousands* of relevant articles over the last 20+ years. This is described in VB6, Winforms and WPF tutorials. [Async in 4.5: Enabling Progress and Cancellation in Async APIs](https://blogs.msdn.microsoft.com/dotnet/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis/) shows how to do this since .NET 4.5. In VB6 and up to .NET 3.5 you needed BackgroundWorker for something similar – Panagiotis Kanavos Feb 20 '18 at 09:19
  • In WPF you don't need and *should not* modify the UI directly. Bind your UI to a model, eg a ProgressModel. You can bind different views to the same model and even change the view while running, eg, a view that only changes the cursor to Cursor.Wait in the beginning and binds its `Progress` property to a status progress bar. If an event arrives after 3 seconds pass, it can use a dialog box view in addition to the status progress bar – Panagiotis Kanavos Feb 20 '18 at 09:24
  • Possible duplicate [How to implement a progress bar using the MVVM pattern](https://stackoverflow.com/questions/3520359/how-to-implement-a-progress-bar-using-the-mvvm-pattern) – Panagiotis Kanavos Feb 20 '18 at 09:25
  • 1
    Dossible duplicate [WPF - display Hourglass when application is busy](https://stackoverflow.com/questions/3480966/display-hourglass-when-application-is-busy) – Panagiotis Kanavos Feb 20 '18 at 09:26

1 Answers1

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;
        }
    }
    );
}
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • Thanks for the response. – Rahim Ali Feb 21 '18 at 14:04
  • Does it work for you? If yes, then you can accept an answer. – Rekshino Feb 21 '18 at 14:06
  • Thanks for the great support. This will work but i am looking some generic custom function which i can call in all my all expensive operations. If i go with above approach i have to copy same code in my each expensive operation and i have huge list of expensive operations. – Rahim Ali Feb 21 '18 at 14:14
  • Thanks Rekshino, in revised code snippet you are passing the list of tasks to the method, if we go with this approach we have to first generate tasks and pass the tasks to this method which is again a major code change. What we are looking is that we generate call to generic custom method in the long running function, which display busy cursor for 3 seconds, if the operation is taking more than 3 seconds a splash screen appear. On operation completion, splash should automatically disappeared. – Rahim Ali Feb 23 '18 at 16:54
  • I have update the question with the approach which we are trying. Any help would be high appreciated. – Rahim Ali Feb 23 '18 at 16:59