Re: Delegates and Classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I have attached a copy of the code at module end:

Module modSystemMonitor
Public Delegate Sub MyDelegate(ByVal intIndex As Integer)

Public frmOver As frmOverview
Public colSystem As New Collection
Public MyDel As MyDelegate

Public Sub LoadSystemId()
Dim n As Integer
Dim Dave As clsSystem
MyDel = AddressOf MyDelegateSub
n = 1
Dave = New clsSystem("Local Test", "Local Test", "WSDEV",
"MONITOR")
AddHandler Dave.DataAvailableEvent, AddressOf
colSystem_NewDataAvailable
Dave.Index = n
colSystem.Add(Dave, Dave.SystemID)
n = 2
Dave = New clsSystem("Backup Domain Controller", "Backup Domain
Controller", "MSS06", "MONITOR")
AddHandler Dave.DataAvailableEvent, AddressOf
colSystem_NewDataAvailable
Dave.Index = n
colSystem.Add(Dave, Dave.SystemID)
End Sub

'*********************************
Public Sub MyDelegateSub(ByVal intIndex As Integer)
frmOver.Label2.Text = "Testing123|" & intIndex

'*** <<This is where I get the Error Cross-Thread Operation Not Valid:
Control 'Label2' accessed form a thread other than the thread it was
created in >> *****

End Sub
'*********************************

Private Sub colSystem_NewDataAvailable(ByVal intIndex As Integer)
Dim Dave As New Data.DataTable
Try
MyDel.Invoke(intIndex)
MsgBox(intIndex)
Catch
MsgBox(Err.Description)
End Try
End Sub
End Module


On Wed, 03 Jan 2007 09:22:52 -0500, Dave <Dave@xxxxxxxxxx> wrote:

Good morning,

I have a class that must trigger an event in a module, which will in
turn update a Label on a form. Due to being in a seperate thread,I
must use a delegate.

Now here is my question. I have managed to create a delegate on a
Form using the Invoke method. In this case though, the class is
actually created as a global variable and it's instance is created in
a module.

What is the best way to implement this delegate since I can't perform
an Invoke from a module?

Thanks for the help,

Dave
.



Relevant Pages