|
-
Jun 25th, 2009, 01:39 PM
#1
Thread Starter
Lively Member
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?
-
Jun 25th, 2009, 02:00 PM
#2
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
 
-
Jun 25th, 2009, 02:35 PM
#3
Re: Delegate vs. Control.CheckForIllegalCrossThreadCalls = False
A delegate should be nearly instant. How many users are you adding to the list?
-
Jun 25th, 2009, 05:32 PM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|