Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

TLS: An exercise in concurrent programming

, 11 May 2008
A walkthough about multi-threading an app and a useful helper class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Threading;

namespace TLS
{

	//=============================================================================================

	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		Timer Timer = null;
		SynchronizationContext _UIContext = null;

		public bool Running
		{
			get { return ( bool ) GetValue( RunningProperty ); }
			set { _UIContext.Send( o => SetValue( RunningProperty, value ), null ); }
		}

		public Window1()
		{
			InitializeComponent();

			_ListBox.SelectedIndex = 0;
			_ListBox.MouseDoubleClick += new MouseButtonEventHandler( _ListBox_MouseDoubleClick );

#if DEBUG
			CommandBindings.Add( new CommandBinding( Command.Break, ( target, e ) => Break() ) );
#endif

			_UIContext = SynchronizationContext.Current;

			btnStop.Click += new RoutedEventHandler( btnStop_Click );

			Timer = new Timer( Timer_Tick, null, 0, 100 );
		}

		Controller _Controller = null;

		void _ListBox_MouseDoubleClick( object sender, MouseButtonEventArgs e )
		{
			if ( !Running ) StartTest();
		}

		void StartTest()
		{
			Running = true;
			_Controller = new Controller( _ListBox.SelectedItem as TestInfo );

			_Controller.Finished += new EventHandler( _Controller_Finished );
			_Controller.Run();
		}

		void btnStop_Click( object sender, RoutedEventArgs e )
		{
			if ( Running )
			{
				Glob.Stop = true;
			}
			else
			{
				StartTest();
			}
		}

		void _Controller_Finished( object sender, EventArgs e )
		{
			UpdateProgressSafe( _Controller.Percent );
			_Controller = null;
			Running = false;
		}

		void Timer_Tick( object state )
		{
			var controller = _Controller;
			if ( controller == null ) return;

			UpdateProgressSafe( _Controller.Percent );
		}

		void UpdateProgressSafe( int percent )
		{
			_UIContext.Send( o => UpdateProgress( percent ), null );
		}

		void UpdateProgress( int percent )
		{
			_ProgressBar.Value = percent;
		}

		class State { public string Value = null; }

		void Break()
		{
			Debugger.Break();
		}

		//-----------------------------------------------------------------------------------------
		// DependencyProperties

		public static readonly DependencyProperty RunningProperty =
			DependencyProperty.Register(
			"Running",
			typeof( bool ),
			typeof( Window1 ),
			new PropertyMetadata( false ) );
	}

	//-----------------------------------------------------------------------------------------
	// RoutedCommands

	public static class Command
	{
		public static readonly RoutedCommand Break = new RoutedCommand();

		static Command()
		{
			Break.InputGestures.Add( new KeyGesture( Key.B, ModifierKeys.Control ) );
		}
	}
	//-----------------------------------------------------------------------------------------
	// Converters

	[ValueConversion( typeof( bool ), typeof( string ) )]
	class RunningConverter : IValueConverter
	{
		public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
		{
			if ( value is bool ) return ( ( bool ) value ) ? "STOP !" : "START !";

			return null;
		}

		public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
		{
			throw new NotImplementedException();
		}
	}

	[ValueConversion( typeof( int ), typeof( string ) )]
	class IntConverter : IValueConverter
	{
		public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
		{
			if ( value is int ) return ( ( int ) value ).ToString( "N0" );

			return null;
		}

		public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
		{
			throw new NotImplementedException();
		}
	}
}

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)

Share

About the Author

Nicholas Butler

United Kingdom United Kingdom

I built my first computer, a Sinclair ZX80, on my 11th birthday in 1980.
In 1992, I completed my Computer Science degree and built my first PC.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing concurrent software professionally, using multi-processor machines, since 1995.
 
In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.
 
I now live near idyllic Bournemouth in England.
 
If you would like help with multithreading, please contact me via my website:
 
 
I can work 'virtually' anywhere!

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 11 May 2008
Article Copyright 2008 by Nicholas Butler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid