UI Threading Helper Classes

The Classes

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);
            }


      }
}

You might also like...

Comments

Roy Osherove Roy Osherove has spent the past 6+ years developing data driven applications for various companies in Israel. He's acquired several MCP titles, written a number of articles on various .NET topics, ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra