Threads, forms, and controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    Threads, forms, and controls

    Is this scenario possible?

    One has a form, a thread and a control. The thread adds a control to the
    form. The form adds handlers to every control added but because the control
    is in a different thread the handler might cause an exception.

    If this is possible how to I prevent it from happening?

    in my form I do

    this.ControlRem oved += new
    System.Windows. Forms.ControlEv entHandler(this .Control_Remove d);

    this.ControlAdd ed += new
    System.Windows. Forms.ControlEv entHandler(this .Control_Added) ;



    private void Control_Added(o bject sender,
    System.Windows. Forms.ControlEv entArgs e)

    {

    e.Control.LostF ocus += new EventHandler(Co ntrol_LostFocus );

    e.Control.Mouse Enter += new EventHandler(Co ntrol_MouseEnte r);

    e.Control.Mouse Leave += new EventHandler(Co ntrol_MouseLeav e);

    }

    private void Control_Removed (object sender,
    System.Windows. Forms.ControlEv entArgs e)

    {

    e.Control.LostF ocus -= new EventHandler(Co ntrol_LostFocus );

    e.Control.Mouse Enter -= new EventHandler(Co ntrol_MouseEnte r);

    e.Control.Mouse Leave -= new EventHandler(Co ntrol_MouseLeav e);

    }





    and I'm worried that it could cause issues ;/



    Thanks,

    Jon


  • Laurent Bugnion

    #2
    Re: Threads, forms, and controls

    Hi,

    Jon Slaughter wrote:
    Is this scenario possible?
    >
    One has a form, a thread and a control. The thread adds a control to the
    form. The form adds handlers to every control added but because the control
    is in a different thread the handler might cause an exception.
    You cannot modify the UI elements from another thread than the UI
    thread. To pass events to the UI thread from another thread, you must
    use the System.Windows. Forms.Control.I nvoke method.


    To check if you need Invoke or not, use the Control.InvokeR equired property.

    HTH
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Jon Slaughter

      #3
      Re: Threads, forms, and controls


      "Laurent Bugnion" <galasoft-lb@bluewin.chwr ote in message
      news:ekH28$bCHH A.3396@TK2MSFTN GP02.phx.gbl...
      Hi,
      >
      Jon Slaughter wrote:
      >Is this scenario possible?
      >>
      >One has a form, a thread and a control. The thread adds a control to the
      >form. The form adds handlers to every control added but because the
      >control is in a different thread the handler might cause an exception.
      >
      You cannot modify the UI elements from another thread than the UI thread.
      To pass events to the UI thread from another thread, you must use the
      System.Windows. Forms.Control.I nvoke method.

      >
      To check if you need Invoke or not, use the Control.InvokeR equired
      property.
      >
      ok, thanks. This means I don't have to worry about that case.

      Jon


      Comment

      Working...