Call event from original thread ??
        Posted  
        
            by user311883
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user311883
        
        
        
        Published on 2010-04-08T12:54:45Z
        Indexed on 
            2010/04/08
            13:13 UTC
        
        
        Read the original article
        Hit count: 271
        
Hi all,
Here is my problem, I have a class which have a object who throw an event and in this event I throw a custom event from my class. But unfortunately the original object throw the event from another thread and so my event is also throw on another thread. This cause a exception when my custom event try to access from controls.
Here is a code sample to better understand :
class MyClass
{
    // Original object
    private OriginalObject myObject;
    // My event
    public delegate void StatsUpdatedDelegate(object sender, StatsArgs args);
    public event StatsUpdatedDelegate StatsUpdated;
    public MyClass()
    {
        // Original object event
        myObject.AnEvent += new EventHandler(myObject_AnEvent);
    }
    // This event is called on another thread
    private void myObject_AnEvent(object sender, EventArgs e)
    {
        // Throw my custom event here
        StatsArgs args = new StatsArgs(..........);
        StatsUpdated(this, args);
    }
}
So when on my windows form I call try to update a control from the event StatsUpdated I get a cross thread exception cause it has been called on another thread.
What I want to do is throw my custom event on the original class thread, so control can be used within it.
Anyone can help me ?
© Stack Overflow or respective owner