Raising and event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVib21pcg==?=

    Raising and event

    Hi,

    I have an app with the main thread and one additional thread.

    In the main thread I defined an event handler MyHandler, the function for
    raisin this event OnMyEvent and the function for processing this event MyFunc.

    I want to raise this event from the second thread. Is it thread safe to call
    the method OnMyEvent from this second thread?

    In Win32 with C++ I waas just sending WM messages between threads. Does work
    OnMyEvent overloaded method the same way, so I could call it from different
    threads?

    public event MyEventHandler MyEvent;
    MyEvent += new MyEventHandler (MyFunc);

    protected virtual void OnMyEvent( EventsArgs e)
    {
    MyEventHandler handler = MyEvent;
    if (handler != null)
    {
    handler(this,e) ;
    }
    }

    void MyFunc( object sender, EventArgs e)
    {
    ....
    }

    Thanks,

    Lubomir
  • Peter Duniho

    #2
    Re: Raising and event

    On Tue, 27 Mar 2007 19:07:24 -0700, Lubomir
    <Lubomir@discus sions.microsoft .comwrote:
    [...]
    I want to raise this event from the second thread. Is it thread safe to
    call the method OnMyEvent from this second thread?
    That depends on what you're actually doing in the event handler.

    There's nothing fundamentally wrong about raising an event from a
    secondary thread. However, a) AFAIK events are just like any other
    variable with respect to thread-safeness, which means that if there's a
    chance one thread may be modifying the event while another thread attempts
    to access it, you need to provide synchronization for that, and b) you do
    need to be aware of the restriction against calling Control methods from a
    thread other than the one in which the Control was created (use Invoke or
    BeginInvoke to get around that).

    The main thing to remember is that the event handler will be executed on
    the thread where the event was raised, even if it belongs to a Form or
    Control created in another thread. All the general rules about
    cross-thread execution apply, but as far as I know there's nothing
    particularly special about the situation.
    In Win32 with C++ I waas just sending WM messages between threads. Does
    work OnMyEvent overloaded method the same way, so I could call it from
    different threads?
    I'm not sure what you mean by "just sending WM messages between threads",
    but note that if you were actually calling SendMessage() from one thread
    using a window handle from a window created on a different thread, that
    was actually against the rules and not reliable. In fact, that
    restriction is the reason behind the .NET restriction against calling
    Control methods from a thread other than the one on which the Control was
    created.

    Pete

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Raising and event

      Lubomir <Lubomir@discus sions.microsoft .comwrote:
      I have an app with the main thread and one additional thread.
      >
      In the main thread I defined an event handler MyHandler, the function for
      raisin this event OnMyEvent and the function for processing this event MyFunc.
      >
      I want to raise this event from the second thread. Is it thread safe to call
      the method OnMyEvent from this second thread?
      Not the way you're doing it at the moment. See
      http://pobox.com/~skeet/csharp/threads/lockchoice.shtml for a thread-
      safe event pattern.
      In Win32 with C++ I waas just sending WM messages between threads. Does work
      OnMyEvent overloaded method the same way, so I could call it from different
      threads?
      No, there's no messages being passed here - events are just a way of
      combining/removing delegates, effectively.

      See http://pobox.com/~skeet/csharp/events.html for more information.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • =?Utf-8?B?THVib21pcg==?=

        #4
        RE: Raising and event

        Thanks for explanation.

        Lubomir


        "Lubomir" wrote:
        Hi,
        >
        I have an app with the main thread and one additional thread.
        >
        In the main thread I defined an event handler MyHandler, the function for
        raisin this event OnMyEvent and the function for processing this event MyFunc.
        >
        I want to raise this event from the second thread. Is it thread safe to call
        the method OnMyEvent from this second thread?
        >
        In Win32 with C++ I waas just sending WM messages between threads. Does work
        OnMyEvent overloaded method the same way, so I could call it from different
        threads?
        >
        public event MyEventHandler MyEvent;
        MyEvent += new MyEventHandler (MyFunc);
        >
        protected virtual void OnMyEvent( EventsArgs e)
        {
        MyEventHandler handler = MyEvent;
        if (handler != null)
        {
        handler(this,e) ;
        }
        }
        >
        void MyFunc( object sender, EventArgs e)
        {
        ...
        }
        >
        Thanks,
        >
        Lubomir

        Comment

        Working...