Results 1 to 3 of 3

Thread: [2005] cross thread operations - events... any nice way to do this?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    [2005] cross thread operations - events... any nice way to do this?

    I'm new to .NET 2
    I have several events that get raised from a thread that is different from my winform thread. I don't want to check InvokeRequired for every control. So I'm guessing it's best to just let the event be raised via a callback on the winform thread instead.....

    I havent used .NET in a while so I cant remember things
    When I'm binding my event handler in the new thread, is there a way to bind the event so it would be called in the winform thread and not in the current thread?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: [2005] cross thread operations - events... any nice way to do this?

    No there is not an automatic way to have the event called in the winform event. If you wrote the object raising the event you could manage something on the RaiseEvent end otherwise you need to check InvokeRequired. I'm not sure what you mean by 'for every control' though.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] cross thread operations - events... any nice way to do this?

    You should take note of how classes like the System.Timers.Timer and FileSystemWatcher work. They have a SynchronisingObject property that tells them what thread to raise their events on. You could use .NET Reflector to see exactly how it's implemented but at a guess it would be something like this:
    vb Code:
    1. Public Event SomeEvent(ByVal sender As Object, ByVal e As EventArgs)
    2.  
    3. Protected Delegate Sub EventRaiser(ByVal e As EventArgs)
    4.  
    5. Protected Sub OnSomeEvent(ByVal e as EventArgs)
    6.     If Me.SynchronizingObject IsNot Nothing AndAlso _
    7.        Me.SynchronizingObject.InvokeRequired Then
    8.         'Raise the event on the thread that owns the synchronising object.
    9.         Me.SynchronizingObject.Invoke(New EventRaiser(AddressOf OnSomeEvent), e)
    10.     Else
    11.         'Raise the event on the current thread.
    12.         RaiseEvent(Me, e)
    13.     End If
    14. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width