Results 1 to 4 of 4

Thread: Delegate vs. Control.CheckForIllegalCrossThreadCalls = False

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    105

    Delegate vs. Control.CheckForIllegalCrossThreadCalls = False

    So I have a custom control that basically acts like a listview, but it draws everything out and finds where the mouse is and all that.

    But when I have another class that goes in a chatroom and receives data, then raises an event to the parent (which is the main form), I then pass it through a delegate and then it adds to the custom control.

    This seems to take a lot longer to add items to the list than If I just use Control.CheckForIllegalCrossThreadCalls = False and bypass the delegate?

    What should I do.


    Code:
     Private Sub ChatClient_UserListItem(ByVal Username As String, ByVal PrimaryIP As UInteger, ByVal PrimaryPort As UShort, ByVal Connection As UShort, ByVal Files As UInteger, ByVal Rank As Byte) Handles ChatClient.UserListItem
             If InvokeRequired = True Then
                 Me.Invoke(New ChatClient_UserListItemSafe(AddressOf ChatClient_UserListItem), Username, PrimaryIP, PrimaryPort, Connection, Files, Rank)
              Else
            'Control.CheckForIllegalCrossThreadCalls = False
            Call AddUser(Username, PrimaryIP, PrimaryPort, Connection, Files, Rank)
            End If
        End Sub

    Is there a faster way to pass all the info, or should I just bypass the cross thread check and not use a delegate?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Delegate vs. Control.CheckForIllegalCrossThreadCalls = False

    Is it safe to bypass the cross thread check? That would be the first question I'd ask, because a race condition, or similar issue, would make your life miserable. Sometimes it is safe, though. You can't be certain when a context switch will happen, so you have to consider all the things that you (or the OS) can do to the control, and what would happen if a context switch caused the background thread to operate at that moment. I would think that a context switch during a Paint event could crash the UI, but it probably won't, in this case.

    A couple alternatives that might make a difference:

    1) I have never compared the difference between Invoking a control vs raising an event in the UI thread via a SynchronizationContext.Post call. It is possible that the SynchronizationContext is faster, or would at least appear faster, as the time might be taken up in the background thread. The idea would be to fill variables from the background thread (the ones you are currently passing to the AddUser call), then Post a call to a sub that does nothing other than either raise an event (which will be raised on the UI thread), or call a function much like your Sub.

    2) I did have a second idea, but I've forgotten it now.
    My usual boring signature: Nothing

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Delegate vs. Control.CheckForIllegalCrossThreadCalls = False

    A delegate should be nearly instant. How many users are you adding to the list?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    105

    Re: Delegate vs. Control.CheckForIllegalCrossThreadCalls = False

    It's about 80 - 100 at a time, but it's being passed from another class,and then the userlist has to draw a 50x50 picture and paint a bunch of rectangles.

    I think I figured it out, using .BeginInvoke works a lot better.

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