Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threads and Events 1

Status
Not open for further replies.

ColdPhreze

Programmer
Apr 15, 2002
93
I need to know how I might go about dynamically creating objects with events for use with a variable number of threads?

For example, Lets say I have a class called SquareClass that squares a number, then when done, throws an event so I can handle the result.

I need to be able to have X number of those classes (preferably in an array or arraylist), so that each thread can have it's own object to work with simultaneously. X could be 5 or 5000.

Here's what I want (and this is a simplified thing, just to demonstrate what I need to do) but it obviously doesn't work because you can't declare arrays withevents:

Code:
Public Class SquareClass
    Public Value As Double
    Public Square As Double

    Public Event ThreadComplete(ByVal Square As Double)

    Public Sub CalcSquare()
        Square = Value * Value
        RaiseEvent ThreadComplete(Square)
    End Sub
End Class


Dim withevents oSquare() as new SquareClass
Dim t() as Thread

Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

    Dim i = 0
    For i = 0 To 100
        t(i) = New Thread(AddressOf oSquare(i).CalcSquare)

        oSquare(i).Value = i
        t(i).Start()
    Next i
End Sub

Sub SquareEventHandler(ByVal Square As Double) _
        Handles oSquare.ThreadComplete

    MsgBox("The square is " & Square)

End Sub

So you see each object would have it's own thread... That way nothing would conflict. Can this be done?

Thanks,
KyferEz

Check out my DeVry Senior Project: and my CarPC Hardware: and my Spam-Fighter forums:
 
this worked for me

Code:
Dim t(1000) As Thread

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim i = 0
        For i = 0 To 50
            Dim n As New SquareClass
            AddHandler n.ThreadComplete, AddressOf SquareEventHandler
            t(i) = New Thread(AddressOf n.calculate)
            n.Value = i
            t(i).Start()
        Next i
    End Sub

    Private Delegate Sub SquareEventHandlerCallback(ByVal square As Double)

    Private Sub SquareEventHandler(ByVal square As Double)
        If InvokeRequired Then
            Invoke(New SquareEventHandlerCallback(AddressOf SquareEventHandler), New Object() {square})
        Else
            Me.TextBox1.AppendText(square & ControlChars.CrLf)
        End If
    End Sub

I didn't array the squareclass but you could. and I added a textbox to show the result so that I could avoid clickcing on ok a hundred times.


Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top