Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Managing busy state with MVVM Light

By: a guest on Nov 28th, 2012  |  syntax: C#  |  size: 5.01 KB  |  views: 43  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace GalaSoft.MvvmLight.Messaging
  10. {
  11.     /// <summary>
  12.     /// Use this class to send a message when the busy state of an application changes.
  13.     /// </summary>
  14.     public class BusyMessage
  15.     {
  16.         /// <summary>
  17.         /// Initializes a new instance of the <see cref="BusyMessage"/> class.
  18.         /// </summary>
  19.         public BusyMessage()
  20.         {
  21.         }
  22.  
  23.         /// <summary>
  24.         /// Gets or sets a value indicating whether the application is currently in a busy state.
  25.         /// </summary>
  26.         public bool IsBusy { get; set; }
  27.        
  28.         /// <summary>
  29.         /// Gets or sets user state information to include with the message.
  30.         /// </summary>
  31.         public object UserState { get; set; }
  32.     }
  33.  
  34.     /// <summary>
  35.     /// Provides a thread-safe controller for managing the busy state of the application.
  36.     /// </summary>
  37.     public class BusyController : GalaSoft.MvvmLight.ViewModelBase
  38.     {
  39.         private readonly object syncRoot = new object();
  40.         private int count;
  41.  
  42.         /// <summary>
  43.         /// Initializes static members of the <see cref="BusyController"/> class.
  44.         /// </summary>
  45.         static BusyController()
  46.         {
  47.             _default = new BusyController();
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Initializes a new instance of the <see cref="BusyController"/> class.
  52.         /// </summary>
  53.         public BusyController()
  54.             : base(Messenger.Default)
  55.         {
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Initializes a new instance of the <see cref="BusyController"/> class.
  60.         /// </summary>
  61.         /// <param name="messenger">Optional. An <see cref="IMessenger"/> which will be used to send messages.</param>
  62.         public BusyController(IMessenger messenger)
  63.             : base(messenger)
  64.         {
  65.         }
  66.  
  67.         private static BusyController _default;
  68.  
  69.         /// <summary>
  70.         /// Gets the default busy controller instance.
  71.         /// </summary>
  72.         public static BusyController Default
  73.         {
  74.             get
  75.             {
  76.                 return _default;
  77.             }
  78.         }
  79.  
  80.         private bool _isBusy;
  81.  
  82.         /// <summary>
  83.         /// Gets a value indicating whether the controller is currently in the busy state.
  84.         /// </summary>
  85.         public bool IsBusy
  86.         {
  87.             get
  88.             {
  89.                 return this._isBusy;
  90.             }
  91.  
  92.             private set
  93.             {
  94.                 if (this._isBusy != value)
  95.                 {
  96.                     this._isBusy = value;
  97.                     this.RaisePropertyChanged(() => this.IsBusy);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Resets the controller.
  104.         /// </summary>
  105.         public void Reset()
  106.         {
  107.             lock (this.syncRoot)
  108.             {
  109.                 this.IsBusy = false;
  110.                 this.count = 0;
  111.  
  112.                 if (this.MessengerInstance != null)
  113.                 {
  114.                     // Send the message indicating that the controller is no longer busy.
  115.                     this.MessengerInstance.Send<BusyMessage>(new BusyMessage() { IsBusy = false });
  116.                 }
  117.             }
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Sends the message
  122.         /// </summary>
  123.         /// <param name="value"><b>true</b> if the application is busy; otherwise, <b>false</b> when the application is no longer busy.</param>
  124.         /// <param name="state">Optional. User state data to include with the message if the controller transitions between busy states.</param>
  125.         public void SendMessage(bool value, object userState = null)
  126.         {
  127.             lock (this.syncRoot)
  128.             {
  129.                 bool send = false;
  130.  
  131.                 if (value)
  132.                 {
  133.                     Interlocked.Increment(ref this.count);
  134.  
  135.                     if (this.count == 1)
  136.                     {
  137.                         this.IsBusy = true;
  138.  
  139.                         // The controller is now busy, send the message.
  140.                         send = true;
  141.                     }
  142.                 }
  143.                 else
  144.                 {
  145.                     if (this.count > 0)
  146.                     {
  147.                         Interlocked.Decrement(ref this.count);
  148.                     }
  149.  
  150.                     if (this.count == 0)
  151.                     {
  152.                         this.IsBusy = false;
  153.  
  154.                         // The controller is no longer busy, send the message.
  155.                         send = true;
  156.                     }
  157.                 }
  158.  
  159.                 if (send && this.MessengerInstance != null)
  160.                 {
  161.                     this.MessengerInstance.Send<BusyMessage>(new BusyMessage() { IsBusy = this.IsBusy, UserState = userState });
  162.                 }
  163.             }            
  164.         }
  165.     }
  166. }