|
-
Jul 13th, 2004, 08:51 PM
#1
Thread Starter
New Member
Threading, Classes, Events?
I'm having problems intercepting events from a class running under multiple threads.
'------------- BEGIN CODE -----------
Public withevents Protocol as clsProtocol
....
.....
.....
dim t as Thread
For i = 0 to servers.count
Protocol = new clsProtocol(servers(i))
t = new Thread(AddressOf Protocol.Query)
t.start()
Thread.Sleep(0)
next i
.......
.....
.......
Private Sub Protocol_TimeOut(byref p as clsProtocol) Handles Protocol.TimeOut
....
....
.....
End Sub
'-------------------------------------------
Essentially the problem is that out of 3 server instances that raise the event only the last instance actually is caught by the handler. I have no idea why or how to work around this problem....
If this is a bit vague I can elaborate.
Cheers,
Tom
-
Jan 10th, 2005, 11:37 AM
#2
Junior Member
Re: Threading, Classes, Events?
did you ever figure this out as i am having a similar issue
-
Jan 11th, 2005, 12:22 AM
#3
Re: Threading, Classes, Events?
According to that code you only have one reference or one instance.
You only declared one instance at the top:
Public withevents Protocol as clsProtocol
And then you keep changing what it references by setting it to new instances:
Protocol = new clsProtocol(servers(i))
Try something like this instead:
VB Code:
Public Protocols as New ArrayList
....
.....
.....
For i = 0 to servers.count
Dim p As New clsProtocol(servers(i))
'Add protocol to list for removing handlers later
Protocols.Add(p)
'attach event
AddHandler p.TimeOut, AddressOf Protocol_TimeOut
Dim t As New Thread(AddressOf Protocol.Query)
t.start()
Thread.Sleep(0)
next i
.......
.....
.......
Private Sub Protocol_TimeOut(byVal p as clsProtocol)
'handle event here
End Sub
Private Sub RemoveHandlers()
'this should be called when you are done with that batch of protocol objects
For Each o As Object In Protocols
Dim p As Protocol=Ctype(o, clsProtocol)
RemoveHandler p.TimeOut, AddressOf Protocol_TimeOut
Protocols.Remove(o)
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|