Reputation: 3795
I have the code below in vb.net, and I am converting it to c#. EDIT Edited out irrelevant code.
but I am stuck at the one with InvokeRequired
. I have added a reference to System.Windows.Forms
and still the code completion doesn't show InvokeRequired
. The listview is on a separate thread to the one I am calling it from, and I need to get around that using Invoke. Can you please tell me what am I missing?
Thank you.
Delegate Sub _AddClient(ByVal client As Socket)
Private Sub AddClient(ByVal client As Socket)
If InvokeRequired Then
Invoke(New _AddClient(AddressOf AddClient), client)
Exit Sub
End If
Dim lvi As New ListViewItem(client.LocalEndPoint.ToString)
lvi.Tag = client
lsvClients.Items.Add(lvi)
End Sub
Upvotes: 1
Views: 7957
Reputation: 44048
Dude, if you're working with WPF, remove all references to System.Windows.Forms
, WPF doesn't need, nor care about that, and you'd better not be confused with classes with equal names from different namespaces and frameworks (for instance System.Windows.Forms.Control
and System.Windows.Control
.
InvokeRequired()
does not exist in WPF, that's replaced by Dispatcher.CheckAccess()
.
Upvotes: 2
Reputation: 17380
Try this:
Delegate Sub _AddClient(ByVal client As Socket)
Private Sub AddClient(ByVal client As Socket)
If ListView1.InvokeRequired Then
Invoke(New _AddClient(AddressOf AddClient), client)
Exit Sub
End If
Dim lvi As New ListViewItem(client.LocalEndPoint.ToString)
lvi.Tag = client
ListView1.Items.Add(lvi)
End Sub
Upvotes: 1
Reputation: 11893
Here is an example of how it is used:
protected void InvokePathDone(Task<IPath<ICoordsCanon>> task,
Action<Task<IPath<ICoordsCanon>>> action) {
if (InvokeRequired) Invoke(action, task);
else action(task);
}
Update for OP question below:
InvokeRequired()
is defined for the Control class to enable callbacks from event delegates to determine if they are, or are not, on the UI thread, and thus whether they must, or must not, perform their action under Invoke().
Upvotes: 0