locked
No actions across threads ??? RRS feed

  • Question

  • I'm not aware of doing more threads in what I'm doing in my application:

    When my main form loads I do this:

    IB_Port.PinChanged+=

    new SerialPinChangedEventHandler(startEmergencyForm);

    just to show another form on the screen to inform the user that the communication is down.

    WHEN the PinChanged-event fires, this should be executed from my main form:

        private void startEmergencyForm(object sender, SerialPinChangedEventArgs e) {
          EmergencySTOP frmSTOP = new EmergencySTOP(IB_Port, IB);
          frmSTOP.BringFormToFrontEvent += EmergencySTOP_BringFormToFrontEvent;
          frmSTOP.ShowDialog(this);
        }
    
    

    but the ShowDialog gives an exception -something like: Action across threads is not allowed (translated from danish language)

    (I know I must have some testing WITCH pin has changed - but never mind that for now !)

    What do I do wrong ?

     

     


    Best regards KSor, Denmark
    Monday, March 7, 2011 4:26 PM

Answers

  • Hi Ksor,

    Though you are not starting any thread, The "PinChanged" event is always raised on a secondary thread. And hence, you cannot access Main Thread UI elements directly. To access UI elements via "PinChanged" handler, you can make use of MethodInvoker as below.

    void startEmergencyForm(object sender, SerialPinChangedEventArgs e)
    {
      MethodInvoker mi = new MethodInvoker(() =>
      {
        EmergencySTOP frmSTOP = new EmergencySTOP(IB_Port, IB);
        frmSTOP.BringFormToFrontEvent += EmergencySTOP_BringFormToFrontEvent;
        frmSTOP.ShowDialog(this);
      });
    
      this.Invoke(mi);
    }
    

    This should solve your problem.

     


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    • Marked as answer by Ksor Tuesday, March 8, 2011 10:28 AM
    Tuesday, March 8, 2011 9:23 AM

All replies

  • This exception is simply because you cannot directly communicate to an UI element from a other threads. For this pupose you can make use of MethodInvoker delegate. You can do something like below,

    private void startEmergencyForm(object sender, SerialPinChangedEventArgs e) 
    {
       MethodInvoker mi = new MethodInvoker(()=>{
    
       EmergencySTOP frmSTOP = new EmergencySTOP(IB_Port, IB);
       frmSTOP.BringFormToFrontEvent += EmergencySTOP_BringFormToFrontEvent;
       frmSTOP.ShowDialog(this);});
    
       this.Invoke(mi);
    }
    
    

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Monday, March 7, 2011 6:00 PM
  • You write: "This exception is simply because you cannot directly communicate to an UI element from a other threads"

    Yeah, that's what the exception tells me, but WHERE do I "communicate to an UI element from another thread" ?

    The application worked very well until this line was added:

    IB_Port.PinChanged+=new SerialPinChangedEventHandler(startEmergencyForm);

    I don't think I start another thread with that or ... ?


    Best regards KSor, Denmark
    Monday, March 7, 2011 7:06 PM
  • Hi Ksor,

    Though you are not starting any thread, The "PinChanged" event is always raised on a secondary thread. And hence, you cannot access Main Thread UI elements directly. To access UI elements via "PinChanged" handler, you can make use of MethodInvoker as below.

    void startEmergencyForm(object sender, SerialPinChangedEventArgs e)
    {
      MethodInvoker mi = new MethodInvoker(() =>
      {
        EmergencySTOP frmSTOP = new EmergencySTOP(IB_Port, IB);
        frmSTOP.BringFormToFrontEvent += EmergencySTOP_BringFormToFrontEvent;
        frmSTOP.ShowDialog(this);
      });
    
      this.Invoke(mi);
    }
    

    This should solve your problem.

     


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    • Marked as answer by Ksor Tuesday, March 8, 2011 10:28 AM
    Tuesday, March 8, 2011 9:23 AM