Library code snippets

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


      }
}

Comments

  1. 23 Mar 2005 at 11:29
    i am an fresh graduate.i need a suggestion abt how to use threading in dcom application.


    how to reduce the loading time.


    then my final one is, how to run a 2 different threads in DCOM application.

    plz friends any one suggest me.

    its my request.

    regards,
    fresh bee
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of UI Threading Helper Classes.

Leave a comment

Sign in or Join us (it's free).

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, ...

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!