在应用程序繁忙时显示沙漏


89

对于使用WPF构造的视图,我想在应用程序繁忙且无响应时将鼠标光标更改为沙漏。

一种解决方案是添加

 this.Cursor = Cursors.Wait;

到所有可能导致UI无法响应的地方。但这显然不是最佳解决方案。我想知道实现这一目标的最佳方法是什么?

是否可以通过使用样式或资源来实现?

谢谢,

Answers:


222

我们做了一个一次性的类,当应用程序要花费很长时间时,它会为我们更改光标,它看起来像这样:

public class WaitCursor : IDisposable
{
    private Cursor _previousCursor;

    public WaitCursor()
    {
        _previousCursor = Mouse.OverrideCursor;

        Mouse.OverrideCursor = Cursors.Wait;
    }

    #region IDisposable Members

    public void Dispose()
    {
        Mouse.OverrideCursor = _previousCursor;
    }

    #endregion
}

我们这样使用它:

using(new WaitCursor())
{
    // very long task
}

可能不是最好的设计,但确实可以解决问题=)


3
使用IDisposable做得好!确保我们始终返回到上一个光标的好方法。
Xavier Poinas,2010年

2
我前段时间有完全相同的想法。但是我将代码包装为UI服务立面类中的私有类,并通过“ ShowWaitCursor”方法返回了它的实例。因此,您必须这样做:using(uiServices.ShowWaitCursor())。看起来很麻烦,但是可以简化单元测试。
Konamiman 2011年

有点题外话:如何正确实施处置... msdn.microsoft.com/en-us/library/ms244737.aspx
Michael Sander

2
@AnoushkaSeechurn WaitCursor()不是方法,而是构造函数。我猜您是否将课程重命名为“ WaitCursor”以外的名称?
卡洛(Carlo)

1
@JánosTigyi我喜欢在自己的区域中分离接口方法。问题是,为什么黑客不使用那里的区域?= P
卡洛(Carlo)

39

我使用这里的答案来构建对我来说更好的东西。问题在于,当Carlo答案中的using块完成时,UI实际上可能仍在忙于数据绑定。由于该块中执行的操作,可能会触发延迟加载的数据或事件。在我的情况下,从等待光标消失有时需要几秒钟,直到UI实际就绪为止。我通过创建一个辅助方法来解决这个问题,该方法设置了waitcursor,还负责设置一个计时器,该计时器将在UI准备好后自动将光标设置回原处。我不能确定这种设计在所有情况下都能正常工作,但对我来说却有效:

    /// <summary>
    ///   Contains helper methods for UI, so far just one for showing a waitcursor
    /// </summary>
    public static class UiServices
    {

    /// <summary>
    ///   A value indicating whether the UI is currently busy
    /// </summary>
    private static bool IsBusy;

    /// <summary>
    /// Sets the busystate as busy.
    /// </summary>
    public static void SetBusyState()
    {
        SetBusyState(true);
    }

    /// <summary>
    /// Sets the busystate to busy or not busy.
    /// </summary>
    /// <param name="busy">if set to <c>true</c> the application is now busy.</param>
        private static void SetBusyState(bool busy)
        {
            if (busy != IsBusy)
            {
                IsBusy = busy;
                Mouse.OverrideCursor = busy ? Cursors.Wait : null;

                if (IsBusy)
                {
                    new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, Application.Current.Dispatcher);
                }
            }
        }

        /// <summary>
        /// Handles the Tick event of the dispatcherTimer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private static void dispatcherTimer_Tick(object sender, EventArgs e)
        {
                var dispatcherTimer = sender as DispatcherTimer;
                if (dispatcherTimer != null)
                {
                    SetBusyState(false);
                    dispatcherTimer.Stop();
                }
        }
    }

+1非常好的代码,您将如何为C#Winforms实现相同的代码。相反:Cursor.Current = Cursors.WaitCursor; //忙碌的Cursor.Current = Cursors.Default;
Zeeshanef

+1非常干净,简单和自动化的设计。如果您正在寻找繁忙的应用程序解决方案,请使用TJ的代码。在进行长时间操作之前,将其扔到Helper类中并像这样使用它:Helpers.UiServices.SetBusyState();
亚伦·里德

13

我只是在做

Mouse.OverrideCursor = Cursors.Wait;
try {
    // Long lasting stuff ...
} finally {
    Mouse.OverrideCursor = null;
}

根据Mouse.OverrideCursor属性的文档

要清除替代游标,请将OverrideCursor设置为null。

try-finally语句可确保在任何情况下都恢复默认的游标,即使发生异常或将try-part保留为returnor时break(如果在循环内)。


长时间的工作可能由于各种原因引发异常。这是一个好方法
aggsol

6

最好的办法是不造成用户界面无响应不断,卸载所有的工作给其他线程/任务合适。

除此之外,您还处于陷阱22:如果您确实添加了一种方法来检测ui无响应,则没有很好的方法来更改光标,因为您需要这样做(偶数线程)没有响应...不过,您可能可以选择标准的win32代码来更改整个窗口的光标,但是?

否则,您将不得不像问题所提示的那样先发制人。


5

我个人比较喜欢看不到鼠标指针从沙漏到箭头的多次切换。为了防止在调用需要花费一些时间的嵌入式函数且每个函数尝试控制鼠标指针时防止这种行为,我使用了一个称为LifeTrackerStack的堆栈(计数器)。而且只有当纸堆为空(计数器为0)时,我才将沙漏放回箭头。

我也使用MVVM。我也更喜欢线程安全代码。

在模型的根类中,我声明我的LifeTrackerStack,我将其填充在子模型类中,或者当我从子模型类中访问它们时直接在子模型类中使用。

我的生活追踪器有2种状态/动作:

  • 活着(计数器> 0)=>将Model.IsBusy设置为true;
  • 完成(计数器== 0)=>将Model.IsBusy设置为false;

然后在我看来,我手动绑定到Model.IsBusy并执行以下操作:

void ModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName == "IsBusy")
    {
        if (this._modelViewAnalysis.IsBusy)
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                this.Cursor = Cursors.Wait;
            }
            else
            {
                Application.Current.Dispatcher.Invoke(new Action(() => this.Cursor = Cursors.Wait));
            }
        }
        else
        {
            Application.Current.Dispatcher.Invoke(new Action(() => this.Cursor = null));
        }
    }

这是我的LifeTrackerStack类:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace HQ.Util.General
{
    /// <summary>
    /// Usage is to have only one event for a recursive call on many objects
    /// </summary>
    public class LifeTrackerStack
    {
        // ******************************************************************
        protected readonly Action _stackCreationAction;
        protected readonly Action _stackDisposeAction;
        private int _refCount = 0;
        private object _objLock = new object();
        // ******************************************************************
        public LifeTrackerStack(Action stackCreationAction = null, Action stackDisposeAction = null)
        {
            _stackCreationAction = stackCreationAction;
            _stackDisposeAction = stackDisposeAction;
        }

        // ******************************************************************
        /// <summary>
        /// Return a new LifeTracker to be used in a 'using' block in order to ensure reliability
        /// </summary>
        /// <returns></returns>
        public LifeTracker GetNewLifeTracker()
        {
            LifeTracker lifeTracker = new LifeTracker(AddRef, RemoveRef);

            return lifeTracker;
        }

        // ******************************************************************
        public int Count
        {
            get { return _refCount; }
        }

        // ******************************************************************
        public void Reset()
        {
            lock (_objLock)
            {
                _refCount = 0;
                if (_stackDisposeAction != null)
                {
                    _stackDisposeAction();
                }
            }
        }

        // ******************************************************************
        private void AddRef()
        {
            lock (_objLock)
            {
                if (_refCount == 0)
                {
                    if (_stackCreationAction != null)
                    {
                        _stackCreationAction();
                    }
                }
                _refCount++;
            }
        }

        // ******************************************************************
        private void RemoveRef()
        {
            bool shouldDispose = false;
            lock (_objLock)
            {
                if (_refCount > 0)
                {
                    _refCount--;
                }

                if (_refCount == 0)
                {
                    if (_stackDisposeAction != null)
                    {
                        _stackDisposeAction();
                    }
                }
            }
        }

        // ******************************************************************
    }
}


using System;

namespace HQ.Util.General
{
    public delegate void ActionDelegate();

    public class LifeTracker : IDisposable
    {
        private readonly ActionDelegate _actionDispose;
        public LifeTracker(ActionDelegate actionCreation, ActionDelegate actionDispose)
        {
            _actionDispose = actionDispose;

            if (actionCreation != null)
                actionCreation();
        }

        private bool _disposed = false;
        public void Dispose()
        {
            Dispose(true);
            // This object will be cleaned up by the Dispose method.
            // Therefore, you should call GC.SupressFinalize to
            // take this object off the finalization queue
            // and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    _actionDispose();
                }

                // Note disposing has been done.
                _disposed = true;
            }
        }
    }
}

以及它的用法:

    _busyStackLifeTracker = new LifeTrackerStack
        (
            () =>
            {
                this.IsBusy = true;
            },
            () =>
            {
                this.IsBusy = false;
            }
        );

我到处都有慢跑,我会:

        using (this.BusyStackLifeTracker.GetNewLifeTracker())
        {
            // long job
        }

这个对我有用。希望对您有帮助!埃里克


@mins,非常感谢。如此赞美,我不得不发布我正在使用的实际解决方案。可能应该解决的故障很少(我不能包括我的所有框架)(应该删除一些代码)。我添加了一个新的答案,它具有更好的代码(我认为),并且更完善,可以适应更多情况。如果需要,可以看看。;-)
Eric Ouellet

3

这里要小心,因为摆弄Wait Cursor可能会导致STA线程出现一些问题。确保如果您使用此工具,那么您将在其自己的线程中进行操作。我在此处发布了一个示例,在STA内部运行,使用它在启动生成的文件时显示WaitCursor,并且不会炸毁(主应用程序)AFAICT。


1

更改光标并不意味着长时间运行的任务完成后,应用程序将不会响应鼠标和键盘事件。为避免用户误导,我使用下面的类从应用程序消息队列中删除所有键盘和鼠标消息。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Input;

public class WpfHourGlass : IDisposable
{

    [StructLayout(LayoutKind.Sequential)]
    private struct POINTAPI
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MSG
    {
        public int hwnd;
        public int message;
        public int wParam;
        public int lParam;
        public int time;
        public POINTAPI pt;
    }
    private const short PM_REMOVE = 0x1;
    private const short WM_MOUSELAST = 0x209;
    private const short WM_MOUSEFIRST = 0x200;
    private const short WM_KEYFIRST = 0x100;
    private const short WM_KEYLAST = 0x108;
    [DllImport("user32", EntryPoint = "PeekMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    private static extern int PeekMessage([MarshalAs(UnmanagedType.Struct)]
    ref MSG lpMsg, int hwnd, int wMsgFilterMin, int wMsgFilterMax, int wRemoveMsg);

    public WpfHourGlass()
    {
        Mouse.OverrideCursor = Cursors.Wait;
        bActivated = true;
    }
    public void Show(bool Action = true)
    {
        if (Action)
        {
            Mouse.OverrideCursor = Cursors.Wait;
        }
        else
        {
            Mouse.OverrideCursor = Cursors.Arrow;
        }

        bActivated = Action;

    }
    #region "IDisposable Support"
    // To detect redundant calls
    private bool disposedValue;
    private bool bActivated;
    // IDisposable
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                //remove todas as mensagens de mouse
                //e teclado que tenham sido produzidas
                //durante o processamento e estejam
                //enfileiradas
                if (bActivated)
                {
                    MSG pMSG = new MSG();
                    while (Convert.ToBoolean(PeekMessage(ref pMSG, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)))
                    {
                    }
                    while (Convert.ToBoolean(PeekMessage(ref pMSG, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE)))
                    {
                    }
                    Mouse.OverrideCursor = Cursors.Arrow;

                }
            }

            // TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            // TODO: set large fields to null.
        }
        this.disposedValue = true;
    }

    public void Dispose()
    {
        // Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    #endregion

}

1

我使用了Olivier Jacot-Descombes的解决方案,它非常简单并且运行良好。谢谢。更新:即使不使用其他线程/后台工作程序,它也可以很好地工作。

我将它与backgroudworker一起使用,忙碌的工作时鼠标光标看起来很棒,工作完成后又恢复正常。

public void pressButtonToDoSomeLongTimeWork()
{    
    Mouse.OverrideCursor = Cursors.Wait;
    // before the long time work, change mouse cursor to wait cursor

    worker.DoWork += doWorkLongTimeAsync;
    worker.RunWorkerCompleted += worker_RunWorkerCompleted;
    worker.RunWorkerAsync();  //start doing some long long time work but GUI can update
}

private void worker_RunWorkerCompleted(object sender,     
                                       RunWorkerCompletedEventArgs e)
{
    //long time work is done();
    updateGuiToShowTheLongTimeWorkResult();
    Mouse.OverrideCursor = null;  //return mouse cursor to normal
}

0

我知道我来晚了,我只是更改了管理应用程序光标“沙漏”(繁忙状态)的方式。

提出的解决方案比我的第一个答案更复杂,但我认为它更完整,更好。

我没有说我有一个简单的解决方案或完整的解决方案。但是对我来说,这是最好的,因为它可以解决我在管理应用程序繁忙状态时遇到的所有麻烦。

优点:

  • 可以从模型和视图管理“忙”状态。
  • 如果没有GUI,则能够管理繁忙状态。与GUI分离。
  • 线程安全(可以在任何线程中使用)
  • 当有一个窗口(对话框)在很长的事务中间应该可见时,支持忙覆盖(暂时显示箭头)。
  • 能够将许多操作与忙碌的行为堆叠在一起,如果这是许多小而长的子任务的一部分,则它们会显示恒定的沙漏。沙漏不会经常从忙碌变为正常或忙碌。如果可能,通过使用堆栈来保持恒定的繁忙状态。
  • 用弱指针支持事件订阅,因为“全局”对象实例是全局的(永远不会被垃圾收集-它是有根的)。

该代码分为几类:

  • 没有GUI类:管理“忙碌”状态的“全局”类,应在应用程序开始时通过调度程序进行初始化。因为它是Global(单例),所以我选择了具有弱的NotifyPropertyChanged事件,以便不对想要通知任何更改的任何人保持强硬引用。
  • GUI类:AppGlobal,它钩接到Global并根据Gloab.Busy状态更改Mouse的外观。程序开始时也应与调度员一起初始化。
  • 一个GUI类,用于帮助Dialog(Window)在进行长事务处理时具有正确的鼠标行为,在该事务中已覆盖了鼠标以显示沙漏,并且在使用Dialog(Window)时需要常规箭头。
  • 该代码还包括一些依赖项。

这是用法:

在里面:

public partial class App : Application
{
    // ******************************************************************
    protected override void OnStartup(StartupEventArgs e)
    {
        Global.Init(Application.Current.Dispatcher);
        AppGlobal.Init(Application.Current.Dispatcher);

首选用法:

        using (Global.Instance.GetDisposableBusyState())
        {
        ...
        }

其他用法:

// ******************************************************************
public DlgAddAggregateCalc()
{
    InitializeComponent();
    Model = DataContext as DlgAddAggregateCalcViewModel;
    this.Activated += OnActivated;
    this.Deactivated += OnDeactivated;
}

// ************************************************************************
private void OnDeactivated(object sender, EventArgs eventArgs)
{
    Global.Instance.PullState();
}

// ************************************************************************
private void OnActivated(object sender, EventArgs eventArgs)
{
    Global.Instance.PushState(false);
}

自动窗口光标:

public partial class DlgAddSignalResult : Window
{
    readonly WindowWithAutoBusyState _autoBusyState = new WindowWithAutoBusyState();

    // ******************************************************************
    public DlgAddSignalResult()
    {
        InitializeComponent();

        Model = DataContext as DlgAddSignalResultModel;

        _autoBusyState.Init(this);
    }

码:

// Copyright (c) 2008 Daniel Grunwald
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

/* Usage:
 * 
 * 
 * 
        public delegate void FileChangedHandler(object sender, FileSystemEventArgs e);

        [field: NonSerialized]
        private readonly SmartWeakEvent<FileChangedHandler> _weakFileChanged = new SmartWeakEvent<FileChangedHandler>();

        public event FileChangedHandler FileChanged
        {
            add
            {
                _weakFileChanged.Add(value);
            }
            remove
            {
                _weakFileChanged.Remove(value);
            }
        }
 *
 *
 */


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace HQ.Util.General.WeakEvent
{
    /// <summary>
    /// A class for managing a weak event.
    /// </summary>
    public sealed class SmartWeakEvent<T> where T : class
    {
        struct EventEntry
        {
            public readonly MethodInfo TargetMethod;
            public readonly WeakReference TargetReference;

            public EventEntry(MethodInfo targetMethod, WeakReference targetReference)
            {
                this.TargetMethod = targetMethod;
                this.TargetReference = targetReference;
            }
        }

        readonly List<EventEntry> eventEntries = new List<EventEntry>();

        // EO: Added for ObservableCollectionWeak
        public int CountOfDelegateEntry
        {
            get
            {
                RemoveDeadEntries();
                return eventEntries.Count;
            }
        }

        static SmartWeakEvent()
        {
            if (!typeof(T).IsSubclassOf(typeof(Delegate)))
                throw new ArgumentException("T must be a delegate type");
            MethodInfo invoke = typeof(T).GetMethod("Invoke");
            if (invoke == null || invoke.GetParameters().Length != 2)
                throw new ArgumentException("T must be a delegate type taking 2 parameters");
            ParameterInfo senderParameter = invoke.GetParameters()[0];
            if (senderParameter.ParameterType != typeof(object))
                throw new ArgumentException("The first delegate parameter must be of type 'object'");
            ParameterInfo argsParameter = invoke.GetParameters()[1];
            if (!(typeof(EventArgs).IsAssignableFrom(argsParameter.ParameterType)))
                throw new ArgumentException("The second delegate parameter must be derived from type 'EventArgs'");
            if (invoke.ReturnType != typeof(void))
                throw new ArgumentException("The delegate return type must be void.");
        }

        public void Add(T eh)
        {
            if (eh != null)
            {
                Delegate d = (Delegate)(object)eh;

                if (d.Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0)
                    throw new ArgumentException("Cannot create weak event to anonymous method with closure.");

                if (eventEntries.Count == eventEntries.Capacity)
                    RemoveDeadEntries();
                WeakReference target = d.Target != null ? new WeakReference(d.Target) : null;
                eventEntries.Add(new EventEntry(d.Method, target));
            }
        }

        void RemoveDeadEntries()
        {
            eventEntries.RemoveAll(ee => ee.TargetReference != null && !ee.TargetReference.IsAlive);
        }

        public void Remove(T eh)
        {
            if (eh != null)
            {
                Delegate d = (Delegate)(object)eh;
                for (int i = eventEntries.Count - 1; i >= 0; i--)
                {
                    EventEntry entry = eventEntries[i];
                    if (entry.TargetReference != null)
                    {
                        object target = entry.TargetReference.Target;
                        if (target == null)
                        {
                            eventEntries.RemoveAt(i);
                        }
                        else if (target == d.Target && entry.TargetMethod == d.Method)
                        {
                            eventEntries.RemoveAt(i);
                            break;
                        }
                    }
                    else
                    {
                        if (d.Target == null && entry.TargetMethod == d.Method)
                        {
                            eventEntries.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
        }

        public void Raise(object sender, EventArgs e)
        {
            int stepExceptionHelp = 0;

            try
            {
                bool needsCleanup = false;
                object[] parameters = {sender, e};
                foreach (EventEntry ee in eventEntries.ToArray())
                {
                    stepExceptionHelp = 1;
                    if (ee.TargetReference != null)
                    {
                        stepExceptionHelp = 2;
                        object target = ee.TargetReference.Target;
                        if (target != null)
                        {
                            stepExceptionHelp = 3;
                            ee.TargetMethod.Invoke(target, parameters);
                        }
                        else
                        {
                            needsCleanup = true;
                        }
                    }
                    else
                    {
                        stepExceptionHelp = 4;
                        ee.TargetMethod.Invoke(null, parameters);
                    }
                }
                if (needsCleanup)
                {
                    stepExceptionHelp = 5;
                    RemoveDeadEntries();
                }

                stepExceptionHelp = 6;
            }
            catch (Exception ex)
            {
                string appName = Assembly.GetEntryAssembly().GetName().Name;
                if (!EventLog.SourceExists(appName))
                {
                    EventLog.CreateEventSource(appName, "Application");
                    EventLog.WriteEntry(appName,
                        String.Format("Exception happen in 'SmartWeakEvent.Raise()': {0}. Stack: {1}. Additional int: {2}.", ex.Message, ex.StackTrace, stepExceptionHelp), EventLogEntryType.Error);
                }

                throw;
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using System.Xml.Serialization;
using HQ.Util.General.Log;
using HQ.Util.General.WeakEvent;
using JetBrains.Annotations;

// using System.Windows.Forms;

// using Microsoft.CSharp.RuntimeBinder;

// ATTENTION: Can only be used with Framework 4.0 and up

namespace HQ.Util.General.Notification
{
    [Serializable]
    public class NotifyPropertyChangedThreadSafeAsyncWeakBase : INotifyPropertyChanged
    {
        // ******************************************************************
        [XmlIgnore]
        [field: NonSerialized]
        public SmartWeakEvent<PropertyChangedEventHandler> SmartPropertyChanged = new SmartWeakEvent<PropertyChangedEventHandler>();

        [XmlIgnore]
        [field: NonSerialized]
        private Dispatcher _dispatcher = null;

        // ******************************************************************
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                SmartPropertyChanged.Add(value);
            }
            remove
            {
                SmartPropertyChanged.Remove(value);
            }
        }

        // ******************************************************************
        [Browsable(false)]
        [XmlIgnore]
        public Dispatcher Dispatcher
        {
            get
            {
                if (_dispatcher == null)
                {
                    _dispatcher = Application.Current?.Dispatcher;
                    if (_dispatcher == null)                    
                    { 
                        if (Application.Current?.MainWindow != null)
                        {
                            _dispatcher = Application.Current.MainWindow.Dispatcher;
                        }
                    }
                }

                return _dispatcher;
            }
            set
            {
                if (_dispatcher == null && _dispatcher != value)
                {
                    Debug.Print("Dispatcher has changed??? ");
                }

                _dispatcher = value;
            }
        }

        // ******************************************************************
        [NotifyPropertyChangedInvocator]
        protected void NotifyPropertyChanged([CallerMemberName] String propertyName = null)
        {
            try
            {
                if (Dispatcher == null || Dispatcher.CheckAccess()) // Can be null in case of Console app
                {
                    SmartPropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName));
                }
                else
                {
                    Dispatcher.BeginInvoke(
                        new Action(() => SmartPropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName))));
                }
            }
            catch (TaskCanceledException ex) // Prevent MT error when closing app...
            {
                Log.Log.Instance.AddEntry(LogType.LogException, "An exception occured when trying to notify.", ex);
            }
        }

        // ******************************************************************
        [NotifyPropertyChangedInvocator]
        protected void NotifyPropertyChanged<T2>(Expression<Func<T2>> propAccess)
        {
            try
            {
                var asMember = propAccess.Body as MemberExpression;
                if (asMember == null)
                    return;

                string propertyName = asMember.Member.Name;

                if (Dispatcher == null || Dispatcher.CheckAccess()) // Can be null in case of Console app
                {
                    SmartPropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName));
                }
                else
                {
                    Dispatcher.BeginInvoke(new Action(() => SmartPropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName))));
                }
            }
            catch (TaskCanceledException ex) // Prevent MT error when closing app...
            {
                Log.Log.Instance.AddEntry(LogType.LogException, "An exception occured when trying to notify.", ex);
            }

        }



        // ******************************************************************
        protected bool UpdateField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                NotifyPropertyChanged(propertyName);
                return true;
            }
            return false;
        }

        // ******************************************************************
    }
}


using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using HQ.Util.General.Notification;

namespace HQ.Util.General
{
    /// <summary>
    /// This is the centralized calss to obtain the Dispatcher in order to make sure to get the appropriate one (the MainWindow dispatcher).
    /// If you need a specidifc thread dispatcher, please refer to System.Windows.Threading.Dispatcher or Dispatcher.CurrentDispatcher.
    /// This dispatcher should be set at the initialization (start) of any GUI executable: ex: Global.Instance.Dispatcher = Application.Current.Dispatcher;

    /// </summary>
    public class Global : NotifyPropertyChangedThreadSafeAsyncWeakBase
    {
        public delegate void IsBusyChangeHandler(bool isBusy);

        /// <summary>
        /// This event happen only the UI thread in low priority
        /// </summary>
        public event IsBusyChangeHandler IsBusyChange;

        private readonly ConcurrentStack<bool> _stackBusy = new ConcurrentStack<bool>();

        // ******************************************************************
        public static void Init(Dispatcher dispatcher)
        {
            Instance.Dispatcher = dispatcher;
        }

        // ******************************************************************
        public static Global Instance = new Global();

        // ******************************************************************
        private Global()
        {
            _busyLifeTrackerStack = new LifeTrackerStack(() => PushState(), PullState);
        }

        // ******************************************************************
        /// <summary>
        /// Will set busy state temporary until object is disposed where 
        /// the state will be back to its previous state.
        /// </summary>
        /// <param name="isBusy"></param>
        /// <returns></returns>
        public LifeTracker GetDisposableBusyState(bool isBusy = true)
        {
            return new LifeTracker(() => PushState(isBusy), PullState);
        }

        // ******************************************************************
        private bool _isBusy;

        /// <summary>
        /// This property should be use by the GUI part in order to control the mouse cursor
        /// </summary>
        public bool IsBusy
        {
            get => _isBusy;

            private set
            {
                if (value == _isBusy) return;
                _isBusy = value;
                Dispatcher.BeginInvoke(new Action(() => IsBusyChange?.Invoke(_isBusy)), DispatcherPriority.ContextIdle);
                NotifyPropertyChanged();
            }
        }

        private readonly object _objLockBusyStateChange = new object();
        // ******************************************************************
        /// <summary>
        /// Always prefer usage of "Using(Global.Instance.GetDisposableBusyState())" whenever possible.
        /// Otherwise ensure to call Pull State to get back previous state of the cursor when action is 
        /// completed
        /// </summary>
        /// <param name="isBusy"></param>
        public void PushState(bool isBusy = true)
        {
            lock (_objLockBusyStateChange)
            {
                _stackBusy.Push(isBusy);
                IsBusy = isBusy;
            }
        }

        // ******************************************************************
        public void PullState()
        {
            lock (_objLockBusyStateChange)
            {
                _stackBusy.TryPop(out bool isBusy);

                if (_stackBusy.TryPeek(out isBusy))
                {
                    IsBusy = isBusy;
                }
                else
                {
                    IsBusy = false;
                }
            }
        }

        // ******************************************************************
        private readonly LifeTrackerStack _busyLifeTrackerStack = null;

        /// <summary>
        /// Only kept for historical reason / compatibility with previous code
        /// </summary>
        [Obsolete("Use direct 'using(Gloabl.Instance.GetDisposableBusyState(isBusy))' which is simpler")]
        public LifeTrackerStack BusyLifeTrackerStack
        {
            get { return _busyLifeTrackerStack; }
        }

        // ******************************************************************
        // private int _latestVersionExecuted = 0;
        private int _currentVersionRequired = 0;
        private readonly object _objLockRunOnce = new object();

        private readonly Dictionary<int, GlobalRunOncePerQueueData> _actionsToRunOncePerQueue =
            new Dictionary<int, GlobalRunOncePerQueueData>();

        private readonly int _countOfRequestInQueue = 0;

        /// <summary>
        /// It will record all action once per key and it
        /// once per Dispatcher queue roll over (on ContextIdle).
        /// When the dispatcher reach the DispatcherPriority.ContextIdle, it will
        /// run all action once.
        /// Thread safe... no action will be lost but can be run twice or more if
        /// some are added by other thread(s) at the same time one is executed.
        /// </summary>
        /// EO: sorry for the name but it is the best found
        /// <param name="key"></param>
        /// <param name="action"></param>
        public void RunOncePerQueueRollOnDispatcherThread(int key, Action action)
        {
            lock (_objLockRunOnce)
            {
                if (!_actionsToRunOncePerQueue.TryGetValue(key, out GlobalRunOncePerQueueData data))
                {
                    data = new GlobalRunOncePerQueueData(action);
                    _actionsToRunOncePerQueue.Add(key, data);
                }

                _currentVersionRequired++;
                data.VersionRequired = _currentVersionRequired;
            }

            if (_countOfRequestInQueue <= 1)
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(ExecuteActions));
            }
        }

        // ******************************************************************
        private void ExecuteActions()
        {
            int versionExecute;

            List<GlobalRunOncePerQueueData> datas = null;
            lock (_objLockRunOnce)
            {
                versionExecute = _currentVersionRequired;
                datas = _actionsToRunOncePerQueue.Values.ToList();
            }

            foreach (var data in datas)
            {
                data.Action();
            }

            lock (_objLockRunOnce)
            {
                List<int> keysToRemove = new List<int>();

                foreach (var kvp in _actionsToRunOncePerQueue)
                {
                    if (kvp.Value.VersionRequired <= versionExecute)
                    {
                        keysToRemove.Add(kvp.Key);
                    }
                }

                keysToRemove.ForEach(k => _actionsToRunOncePerQueue.Remove(k));

                if (_actionsToRunOncePerQueue.Count == 0)
                {
                    // _latestVersionExecuted = 0;
                    _currentVersionRequired = 0;
                }
                else
                {
                    // _latestVersionExecuted = versionExecute;
                }
            }
        }

        // ******************************************************************
    }
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using HQ.Util.General;
using HQ.Util.General.Notification;
using Microsoft.VisualBasic.Devices;
using System.Windows;
using Mouse = System.Windows.Input.Mouse;
using System.Threading;

namespace HQ.Wpf.Util
{
    public class AppGlobal
    {
        // ******************************************************************
        public static void Init(Dispatcher dispatcher)
        {
            if (System.Windows.Input.Keyboard.IsKeyDown(Key.LeftShift) || System.Windows.Input.Keyboard.IsKeyDown(Key.RightShift))
            {
                var res = MessageBox.Show($"'{AppInfo.AppName}' has been started with shift pressed. Do you want to wait for the debugger (1 minute wait)?", "Confirmation", MessageBoxButton.YesNo,
                    MessageBoxImage.Exclamation, MessageBoxResult.No);

                if (res == MessageBoxResult.Yes)
                {
                    var start = DateTime.Now;

                    while (!Debugger.IsAttached)
                    {
                        if ((DateTime.Now - start).TotalSeconds > 60)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }
                }
            }

            if (dispatcher == null)
            {
                throw new ArgumentNullException();
            }

            Global.Init(dispatcher);
            Instance.Init();
        }

        // ******************************************************************
        public static readonly AppGlobal Instance = new AppGlobal();

        // ******************************************************************
        private AppGlobal()
        {
        }

        // ******************************************************************
        private void Init()
        {
            Global.Instance.PropertyChanged += AppGlobalPropertyChanged;
        }

        // ******************************************************************
        void AppGlobalPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsBusy")
            {
                if (Global.Instance.IsBusy)
                {
                    if (Global.Instance.Dispatcher.CheckAccess())
                    {
                        Mouse.OverrideCursor = Cursors.Wait;
                    }
                    else
                    {
                        Global.Instance.Dispatcher.BeginInvoke(new Action(() => Mouse.OverrideCursor = Cursors.Wait));
                    }
                }
                else
                {
                    if (Global.Instance.Dispatcher.CheckAccess())
                    {
                        Mouse.OverrideCursor = Cursors.Arrow;
                    }
                    else
                    {
                        Global.Instance.Dispatcher.BeginInvoke(new Action(() => Mouse.OverrideCursor = Cursors.Arrow));
                    }
                }
            }
        }

        // ******************************************************************
    }
}



using HQ.Util.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace HQ.Wpf.Util
{
    /// <summary>
    /// Ensure window cursor is "normal" (arrow) when visible.
    /// Usage: In Window class, define a member: OverrideCursorMtForWindow. Instantiate in constructor after Initialisation.
    /// </summary>
    public class WindowWithAutoBusyState
    {
        // ******************************************************************
        Window _window;
        bool _nextStateShoulBeVisible = true;

        // ******************************************************************
        public WindowWithAutoBusyState()
        {

        }

        // ******************************************************************
        public void Init(Window window)
        {
            _window = window;

            _window.Cursor = Cursors.Wait;
            _window.Loaded += (object sender, RoutedEventArgs e) => _window.Cursor = Cursors.Arrow;

            _window.IsVisibleChanged += WindowIsVisibleChanged;
            _window.Closed += WindowClosed;
        }

        // ******************************************************************
        private void WindowIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (_window.IsVisible)
            {
                if (_nextStateShoulBeVisible)
                {
                    Global.Instance.PushState(false);
                    _nextStateShoulBeVisible = false;
                }
            }
            else
            {
                if (!_nextStateShoulBeVisible)
                {
                    Global.Instance.PullState();
                    _nextStateShoulBeVisible = true;
                }
            }
        }

        // ******************************************************************
        private void WindowClosed(object sender, EventArgs e)
        {
            if (!_nextStateShoulBeVisible)
            {
                Global.Instance.PullState();
                _nextStateShoulBeVisible = true;
            }
        }

        // ******************************************************************

    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.