InvokeRequired Help

OSVBNET 1,386 Reputation points
2022-08-22T17:50:58.893+00:00

Hello experts
I'm using this code to enable/disable controls in a thread:

Delegate Sub SetEnabledCallback(ByVal Enabled As Boolean)
Private Sub SetEnabled(ByVal Enabled As Boolean)

If PrintButton.InvokeRequired Then
Invoke(Sub() PrintButton.Enabled = Enabled)
Else
PrintButton.Enabled = Enabled
End If

Please advise how to perform this double inner check:

If MyComboBox.SelectedIndex = 0 Then
If MyCheckBox.InvokeRequired Then
Invoke(Sub() MyCheckBox.Enabled = Enabled)
Else
MyCheckBox.Enabled = Enabled
End If
End If

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,566 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.8K Reputation points
    2022-08-22T18:25:59.657+00:00

    In my opinion, this should work:

    Invoke( Sub( )  
          If MyComboBox.SelectedIndex = 0 Then  
             MyCheckBox.Enabled = Enabled  
          End If  
       End Sub  
    

    The InvokeRequired is not checked because you are using threads. Even if Invoke is not needed, I do not think that using Invoke is an error.

    Perhaps there are other opinions on this subject.

    0 comments No comments