Results 1 to 3 of 3

Thread: Threading, Classes, Events?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    14

    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
    Cheers,

    Black Thunder

  2. #2
    Junior Member
    Join Date
    Jul 2002
    Location
    somewhere you don't want to be
    Posts
    29

    Re: Threading, Classes, Events?

    did you ever figure this out as i am having a similar issue

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    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:
    1. Public Protocols as New ArrayList
    2.  
    3. ....
    4. .....
    5. .....
    6.  
    7. For i = 0 to servers.count
    8.  
    9. Dim p As New clsProtocol(servers(i))
    10. 'Add protocol to list for removing handlers later
    11. Protocols.Add(p)
    12. 'attach event
    13. AddHandler p.TimeOut, AddressOf Protocol_TimeOut
    14. Dim t As New Thread(AddressOf Protocol.Query)
    15. t.start()
    16. Thread.Sleep(0)
    17.  
    18. next i
    19.  
    20. .......
    21. .....
    22. .......
    23.  
    24. Private Sub Protocol_TimeOut(byVal p as clsProtocol)
    25.    'handle event here
    26. End Sub
    27.  
    28. Private Sub RemoveHandlers()
    29.    'this should be called when you are done with that batch of protocol objects
    30.    For Each o As Object In Protocols
    31.       Dim p As Protocol=Ctype(o, clsProtocol)
    32.       RemoveHandler p.TimeOut, AddressOf Protocol_TimeOut
    33.       Protocols.Remove(o)
    34.    Next
    35. 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
  •  



Click Here to Expand Forum to Full Width