Invoking components

I'm currently trying to make calls to ToolStripButtons and ToolStripMenuItems (to enable/disable), but the calls are not currently thread-safe, in that the calls to enable or disable the buttons and items are coming from a different thread. Unfortunately, ToolStripButtons and ToolStripMenuItems don't have an InvokeRequired property, or BeginInvoke and EndInvoke methods.

Does anyone have any ideas how to make calls to these items thread-safe?

Thanks for any info.

-Jason

[482 byte] By [OpticTygre] at [2007-12-16]
# 1
If you are putting them in a Form or UserControl, call the BeginInvoke on that instead. Then you are guaranteeed to be in the UI thread and can manipulate the objects.
FrankHileman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Since you should not modify the UI from an alternate thread I would suggest using Events to enable/disable your toolstrip items...
DMan1 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Actually, I was using events to change them. The problem was, the events were being fired from a different thread, and that's what caused the cross-threading errors.

I actually found a simple solution. Instead of testing for InvokeRequired on the ToolStripButtons and ToolMenuItems, I checked the InvokeRequired property of the actual ToolStrip and MenuStrip items themselves. After that, it seemed to work perfectly.

Example:

Delegate Sub ChangeButtonDelegate(ByVal Item as ToolStripButton)

Private Sub ChangeButton(ByVal Item as ToolStripButton)
If ToolStrip1.InvokeRequired Then
Dim d as New ChangeButtonDelegate(AddressOf ChangeButton)
Me.Invoke(d, Item)
Else
Item.Enabled = Not (Item.Enabled)
End If
End Sub

Works just fine!
-Jason

OpticTygre at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...