How to raise an event in UI thread?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sin Jeong-hun

    How to raise an event in UI thread?

    Suppose class Engine do something in another thread and raise events.

    class Engine
    {
    Thread Worker;
    public event ... EngineMessage;
    public void Start()
    {
    Worker=new Thread(new ThreadStart(Run ));
    Worker.Start();
    }
    private Run()
    {
    ..Do some length work....
    under some condition, fires
    this.EngineMess age(msg);
    }
    }

    If I subscribe to this event in the Form and tries to set the message
    to a TextBox, this causes InvalidOperatio nExeption since this message
    is from another thread. I can use this.InvokeRequ red at the form, but
    can't I do this at the Engine class so that at the Form, I can handle
    the message without using delegates? I mean in the Engine class , can
    I somehow raise the event in the same thread as the one called Start()?

  • Marc Bartsch

    #2
    Re: How to raise an event in UI thread?

    Hi Sin,

    Sin Jeong-hun wrote:
    Suppose class Engine do something in another thread and raise events.
    >
    class Engine
    {
    Thread Worker;
    public event ... EngineMessage;
    public void Start()
    {
    Worker=new Thread(new ThreadStart(Run ));
    Worker.Start();
    }
    private Run()
    {
    ..Do some length work....
    under some condition, fires
    this.EngineMess age(msg);
    }
    }
    >
    If I subscribe to this event in the Form and tries to set the message
    to a TextBox, this causes InvalidOperatio nExeption since this message
    is from another thread. I can use this.InvokeRequ red at the form, but
    can't I do this at the Engine class so that at the Form, I can handle
    the message without using delegates? I mean in the Engine class , can
    I somehow raise the event in the same thread as the one called Start()?
    >
    I am not sure whether you can raise the event on the UI thread without a reference to the UI, but you can do the following in your form that processes the EngineMessage event without explicitly defining so many delegates:

    class MyForm : Form
    {
    // Process EngineMessage here
    protected void Engine_EngineMe ssage(string msg)
    {
    // Invoke SetText on UI thread using an anonymous method and
    // cast it to MethodInvoker.
    this.Invoke((Me thodInvoker)del egate { SetText(msg); });
    }

    private void SetText(string txt)
    {
    this.myTextBox. Text = txt;
    }
    }

    Hope it helps,

    Marc.

    Comment

    • Peter Duniho

      #3
      Re: How to raise an event in UI thread?

      Marc Bartsch wrote:
      I am not sure whether you can raise the event on the UI thread without a
      reference to the UI, but you can do the following in your form that
      processes the EngineMessage event without explicitly defining so many
      delegates:
      >
      class MyForm : Form
      {
      // Process EngineMessage here
      protected void Engine_EngineMe ssage(string msg)
      {
      // Invoke SetText on UI thread using an anonymous method and
      // cast it to MethodInvoker.
      this.Invoke((Me thodInvoker)del egate { SetText(msg); });
      }
      Or, if it's really a simple method like that (it's not always, but in
      this example obviously it is :) ):

      // Process EngineMessage here
      protected void Engine_EngineMe ssage(string msg)
      {
      // Invoke SetText on UI thread using an anonymous method and
      // cast it to MethodInvoker.
      this.Invoke((Me thodInvoker)del egate { this.myTextBox. Text = msg });
      }

      Actually, for that matter, unless I needed to use the same code
      somewhere else by calling it as a method, I would probably just put the
      whole invoke-necessary code in the anonymous delegate. It's just that
      as the anonymous delegate gets longer, the odds that it's doing
      something that needs to be shared by non-invoking code goes up, thus
      justifying putting it into a shareable method. :)

      Pete

      Comment

      Working...