Raising event to a different thread

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

    #1

    Raising event to a different thread

    Hi,
    I am relatively new at threads. I have a windows form and a class running
    on the same thread. The class has a background task that needs to be done
    every minute on a background thread. I am still deciding on whether to use
    System.Threadin g.Timer or Thread.Start/Thread.Sleep. The background task may
    then raise an event which will be captured by the windows form. I believe
    this event needs to be raised on the windows form thread. How do I go about
    achieving this?
  • Cor Ligthert

    #2
    Re: Raising event to a different thread

    Jrh,

    When you want to use threading, ask yourself first why do I want to use
    threading.
    Be aware that it cost you at least more processing time.

    When it are processes that take less than let say 3 seconds (and that is
    terrible long for a modern computer), than it is already direct senseless to
    use threading.

    I hope this gives an idea.

    Cor


    Comment

    • Armin Zingler

      #3
      Re: Raising event to a different thread

      "jrh" <jrh@discussion s.microsoft.com > schrieb[color=blue]
      > Hi,
      > I am relatively new at threads. I have a windows form and a class
      > running on the same thread. The class has a background task that
      > needs to be done every minute on a background thread. I am still
      > deciding on whether to use System.Threadin g.Timer or
      > Thread.Start/Thread.Sleep. The background task may then raise an
      > event which will be captured by the windows form. I believe this
      > event needs to be raised on the windows form thread. How do I go
      > about achieving this?[/color]

      In the event handler, use the Form's Invoke/BeginInvoke method to marshal
      the call to the other thread.

      Armin

      Comment

      • jrh

        #4
        Re: Raising event to a different thread

        Thanks for the reply. I'm developing in layers i.e. User Interface layer,
        Business Object Layer and Data layer. The task that I want to perform is
        part of a business object. I would like to encapsulate it so the business
        object event is raised on its thread (which is the same as the UI) instead of
        the background thread. Therefore
        the UI developer does not have to worry about calling Invoke. Also the UI
        developer may not even be aware that threads are being used.

        An idea I have is to add a private control to the business object. Then I
        could use invoke to then raise the event on the business objects original
        thread.
        Will this work? Is there another method of marshalling a call to another
        thread?

        Comment

        Working...