Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#

IDispatch

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
22 Aug 2009CPOL4 min read 36.8K   309   31  
Pattern to easily create and test asynchronous solutions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using SlasheneFramework;
using System.ComponentModel;

namespace DispatcherApplication
{
	public class PersonsViewModel : INotifyPropertyChanged
	{
		readonly IDispatcher _CurrentDispatcher;
		readonly IDispatcher<IPersonRepository> _Repository;
		public PersonsViewModel(IDispatcher<IPersonRepository> repository, IDispatcher currentDispatcher)
		{
			_Persons = new ObservableCollection<string>();
			_Repository = repository;
			_CurrentDispatcher = currentDispatcher ?? new WpfDispatcher();
			DoRefresh();
		}
		public PersonsViewModel(IDispatcher<IPersonRepository> repository)
			: this(repository, null)
		{
		}

		private readonly ObservableCollection<string> _Persons;

		public ObservableCollection<string> Persons
		{
			get
			{
				return _Persons;
			}
		}

		private bool _IsLoading;
		public bool IsLoading
		{
			get
			{
				return _IsLoading;
			}
			set
			{
				if(value != _IsLoading)
				{
					_IsLoading = value;
					if(PropertyChanged != null)
						PropertyChanged(this, new PropertyChangedEventArgs("IsLoading"));
				}
			}
		}

		protected void DoRefresh()
		{
			IsLoading = true;
			_Repository.BeginInvoke((IPersonRepository repo) =>
			{
				var persons = repo.GetPersonsName();
				_CurrentDispatcher.BeginInvoke(() =>
				{
					Persons.Clear();
					foreach(var person in persons)
					{
						Persons.Add(person);
					}
					IsLoading = false;
				});
			});
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions