.Net: Delegates

  • Thread starter Thread starter ~J~
  • Start date Start date

~J~

~J~

Soldato
Joined
20 Oct 2003
Posts
7,558
Location
London
Never ventured into Delegates much but of late I'm having to re-code some apps that use threading more and more.

I understand (or I think I do) the reasoning behind Delegates and why they are needed but am stuck at this simple little problem:

I have a form within a class library that I'm calling from the main program.

To use a very simple example, I want to test that things are working by updating a variable on the thread, so have the following:

Public Delegate Sub CallBackDelegate(ByVal sb As System.Windows.Media.Brush)

Public Sub updateUI(ByVal sb As System.Windows.Media.Brush)
Dim a As New CallBackDelegate(AddressOf uui)
a.Invoke(sb)
End Sub

Private Sub uui(ByVal sb As System.Windows.Media.Brush)
Me.ColourOverlay = sb
End Sub

On the main form itself, I have the following code:

Private Shared Sub UpdateFromConfiguration()
Dim s As System.Windows.Media.Brush = System.Windows.Media.Brushes.Azure
naw.updateUI(s)
End Sub

Now that doesn't work, I still get the error about not being able to update because it's on a seperate thread, but, can someone explain to me what I'm doing wrong.
 
I don't know VB.NET, but it looks like you're confusing Delegate.Invoke and Form.Invoke. Delegate.Invoke has nothing to do with threading - it's just like a normal method call.

Form.Invoke makes sure that code is ran on the UI thread. Have a look at MSDN :)
 
Thanks wush.

Having done a bit more research it appears I'm probably looking at the wrong area anyway because it's a WPF and the use of BackgroundWorkers supercedes Threads.
 
Back
Top Bottom