Advertisement
Guest User

Busy state management with MVVM Light

a guest
Nov 28th, 2012
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.99 KB | None | 0 0
  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(Messenger.Default);
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Initializes a new instance of the <see cref="BusyController"/> class.
  52.         /// </summary>
  53.         public BusyController()
  54.         {
  55.         }
  56.  
  57.         /// <summary>
  58.         /// Initializes a new instance of the <see cref="BusyController"/> class.
  59.         /// </summary>
  60.         /// <param name="messenger">Optional. An <see cref="IMessenger"/> which will be used to send messages.</param>
  61.         public BusyController(IMessenger messenger)
  62.             : base(messenger)
  63.         {
  64.         }
  65.  
  66.         private static BusyController _default;
  67.  
  68.         /// <summary>
  69.         /// Gets the default busy controller instance.
  70.         /// </summary>
  71.         public static BusyController Default
  72.         {
  73.             get
  74.             {
  75.                 return _default;
  76.             }
  77.         }
  78.  
  79.         private bool _isBusy;
  80.  
  81.         /// <summary>
  82.         /// Gets a value indicating whether the controller is currently in the busy state.
  83.         /// </summary>
  84.         public bool IsBusy
  85.         {
  86.             get
  87.             {
  88.                 return this._isBusy;
  89.             }
  90.  
  91.             private set
  92.             {
  93.                 if (this._isBusy != value)
  94.                 {
  95.                     this._isBusy = value;
  96.                     this.RaisePropertyChanged(() => this.IsBusy);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Resets the controller.
  103.         /// </summary>
  104.         public void Reset()
  105.         {
  106.             lock (this.syncRoot)
  107.             {
  108.                 this.IsBusy = false;
  109.                 this.count = 0;
  110.  
  111.                 if (this.MessengerInstance != null)
  112.                 {
  113.                     // Send the message indicating that the controller is no longer busy.
  114.                     this.MessengerInstance.Send<BusyMessage>(new BusyMessage() { IsBusy = false });
  115.                 }
  116.             }
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Sends the message
  121.         /// </summary>
  122.         /// <param name="value"><b>true</b> if the application is busy; otherwise, <b>false</b> when the application is no longer busy.</param>
  123.         /// <param name="state">Optional. User state data to include with the message if the controller transitions between busy states.</param>
  124.         public void SendMessage(bool value, object userState = null)
  125.         {
  126.             lock (this.syncRoot)
  127.             {
  128.                 bool send = false;
  129.  
  130.                 if (value)
  131.                 {
  132.                     Interlocked.Increment(ref this.count);
  133.  
  134.                     if (this.count == 1)
  135.                     {
  136.                         this.IsBusy = true;
  137.  
  138.                         // The controller is now busy, send the message.
  139.                         send = true;
  140.                     }
  141.                 }
  142.                 else
  143.                 {
  144.                     if (this.count > 0)
  145.                     {
  146.                         Interlocked.Decrement(ref this.count);
  147.                     }
  148.  
  149.                     if (this.count == 0)
  150.                     {
  151.                         this.IsBusy = false;
  152.  
  153.                         // The controller is no longer busy, send the message.
  154.                         send = true;
  155.                     }
  156.                 }
  157.  
  158.                 if (send && this.MessengerInstance != null)
  159.                 {
  160.                     this.MessengerInstance.Send<BusyMessage>(new BusyMessage() { IsBusy = this.IsBusy, UserState = userState });
  161.                 }
  162.             }            
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement