using System;
using System.Collections;
using System.Windows.Forms;
namespace Royo.UIThreading
{
///
/// Manages a collection of UIThreadHandlers
/// And allows easy creation of UIThreadHandler Instances
///
public class UIThreadManager
{
//Priate collection of Thread Handlers
protected ArrayList m_ThreadHandlers = new ArrayList();
///
/// All instances of UIThreadHandlers currently in memory
///
public ArrayList ThreadHandlers
{
get
{
return m_ThreadHandlers;
}
}
public UIThreadManager()
{
}
///
/// Creates and instance of a UIThreadHandler class
/// and adds it to the collection of current Handlers
///
/// The target Control on which the new handler will invoke a delegate
/// The target delegate whcih will be invoked on the Control instance
/// The created handler
public UIThreadHandler CreateHandler(Control TargetControl,EventHandler HandlerDelegateInControl)
{
UIThreadHandler handler = new UIThreadHandler(TargetControl,HandlerDelegateInControl);
m_ThreadHandlers.Add(handler);
return handler;
}
///
/// Creates and instance of a UIThreadHandler class
/// and adds it to the collection of current Handlers
///
/// The target Control on which the new handler will invoke a delegate
/// The target delegate whcih will be invoked on the Control instance
/// The created handler
public UIThreadHandler CreateHandler(Control TargetControl,Delegate HandlerDelegateInControl)
{
UIThreadHandler handler = new UIThreadHandler(TargetControl,HandlerDelegateInControl);
m_ThreadHandlers.Add(handler);
return handler;
}
///
/// Creates an instance of a new UIThreadHandler Class
/// And adds it to the collection of handlers.
/// Then returns a new delegate of type EventHandler
/// which points to a method on the new UIThreadHandler.
///
/// The target Control on which the new handler will invoke a delegate
/// The target delegate whcih will be invoked on the Control instance
/// An EventHandler Delegate to a method on the UIThread Handler
public EventHandler NewHandler(Control TargetControl,EventHandler HandlerDelegateInControl)
{
UIThreadHandler handler = CreateHandler(TargetControl,HandlerDelegateInControl);
return new EventHandler(handler.BaseEventHandler);
}
///
/// Removes a UIThreadHandler from the Collection of UIHandlers
///
/// UIThreadHandler Instance to remove
public void RemoveHandler(UIThreadHandler handler)
{
try
{
m_ThreadHandlers.Remove(handler);
}
catch(Exception e){}
}
///
/// Add a custom handler to the current collection
///
///
public void AddHandler(UIThreadHandler handler)
{
try
{
m_ThreadHandlers.Add(handler);
}
catch(Exception e){}
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////UIThreadHandler/////////////////
//////////////////////////////////////////////////////////////////
///
/// This class represents an instance of a UI method caller
/// Its only job is to recieve a delegate found on
/// an instance of a control(usually a form) and to invoke that delegate
/// When it recieved an event
///
public class UIThreadHandler
{
//The delegate which is called on the UI thread using "m_TargetControl.Invoke()"
protected System.Delegate m_ControlHandler;
//The Control (Usualoy A Form) on which to Invoke a UI delegate
protected Control m_TargetControl;
///
/// Creates an instance of UIThreadHandler
/// Along with its associated Form and Delegate
/// on which to invoke UI thread-related events
///
/// The Form instance on which UI actions are performed
/// A delegate pointing to a method on the Form instance
/// Which updates a control on the Form
///
/// EventThrower e = new EventThrower();
/// e.OnEventThrow+=new EventHandler(m_uimgr.CreateHandler(this,new ///EventHandler(Handler11)).BaseEventHandler);
/// e.Start();
///
public UIThreadHandler(Control TargetControl,Delegate HandlerDelegateInControl)
{
m_TargetControl= TargetControl;
m_ControlHandler = HandlerDelegateInControl;
}
///
/// A method which matches most basic event handlers
/// Use this to create a delegate which recieves events
/// which need to trigger UI actions in the form thread.
///
///
///
public void BaseEventHandler(object source, EventArgs args)
{
try
{
if(m_ControlHandler!=null)
{
object[] arr = new object[]{source,args};
m_TargetControl.Invoke(m_ControlHandler,arr);
}
}
catch(Exception e)
{
string se =e.ToString();
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
public void BaseEventHandler(object source, object args)
{
if(m_ControlHandler!=null)
{
object[] arr = new object[]{source,args};
m_TargetControl.Invoke(m_ControlHandler,arr);
}
}
public void BaseEventHandler()
{
if(m_ControlHandler!=null)
{
m_TargetControl.Invoke(m_ControlHandler);
}
}
public void BaseEventHandler(int arg)
{
if(m_ControlHandler!=null)
{
m_TargetControl.Invoke(m_ControlHandler,new object[]{arg});
}
}
public void BaseEventHandler(long arg)
{
if(m_ControlHandler!=null)
{
m_TargetControl.Invoke(m_ControlHandler,new object[]{arg});
}
}
public void BaseEventHandler(object arg)
{
if(m_ControlHandler!=null)
{
m_TargetControl.Invoke(m_ControlHandler,new object[]{arg});
}
}
public void BaseEventHandler(string arg)
{
if(m_ControlHandler!=null)
{
m_TargetControl.Invoke(m_ControlHandler,new object[]{arg});
}
}
///
/// Explicitly Invoke the Delegate on the Form Instance
/// using the specifies parameters
///
/// any parameters whcih need to be sent to the
/// delegate on the Form Instance
public void Invoke(params object[] args)
{
m_TargetControl.Invoke(m_ControlHandler,args);
}
}
}
UI Threading Helper Classes
By Roy Osherove, published on 01 Apr 2004
| Filed in
The Classes
You might also like...
Threading forum discussion
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
gamedominoqiuqiuqq
by shielaariel888 (0 replies)
-
how to find job
by jobsflyer123 (0 replies)
-
Exporting data from a repeater to an Excel sheet
by TomBlack (28 replies)
-
Calculate Date of Birth from Age
by k.prashanth (7 replies)
Threading podcasts
-
.NET Rocks: Stories from CodeMash!
Published 8 years ago, running time 0h56m
While at CodeMash, Carl and Richard collected some great stories. First up is Evan Huack, who digs into cudafy, a library on codeplex that allows you to run C# code (any IL code actually) on your GPU. The second story is about Jessie Shternshus and her company The Improv Effect. Jessie talks abou.
Threading jobs
-
.NET, C#, MVC Senior Software Developer
Frontline Technologies, Inc. in Exton, United States
-
C#, JQuery, KnockoutJs. Nhibernate. Unique 3-6 month opportunity in Kitesurfing/Windsurfing heaven
RWI in Cabarete, Dominican Republic
$12,000-12,000 USD per year (Contract) -
C# / .NET Programmer
Mews Systems in Prague, Czech Republic
-
Experienced C# Developer
Zenco Systems Ltd in Rugby, United Kingdom
Annual Bonus, Annual Salary Review, Perfomance Related Bonus
Comments