invoke and begininvoke

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lore Leunoeg

    #1

    invoke and begininvoke

    If I call a delegates BeginInvoke a new thread from the threadpool is
    started. But what happens if I call the Invoke method? Does this also start
    a new thread?

    Thank you
    sincerely
    Lore


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: invoke and begininvoke

    Lore,
    | But if the Invoke method is like calling the method directly. Why does
    this
    | example use delegates? Do you have got an idea?
    It allows how the two items are compared to be changed.

    Consider the following:

    Function CompareLessThan (ByVal X As Integer, _
    ByVal Y As Integer) As Boolean
    If X < Y Then
    Return True
    Else
    Return False
    End If
    End Function

    | Dim Sort As New SortClass()
    | Dim arr1() As Integer = {1, 5, 3, 2, 7, 22, 5, 54, 12}
    Sort.SelectionS ort(AddressOf Sort.CompareLes sThan, arr1)
    | MsgBox("Array sorted.")

    Notice the SelectionSort routine didn't changed, however now the values are
    descending instead of ascending.

    Granted in the case of Integers its not as important as the case of Domain
    Objects

    | Delegate Function Compare(ByVal x As Person, _
    | ByVal y As Person) As Boolean

    | Sub SelectionSort(B yVal comparison As Compare, _
    | ByVal people() As Person)

    Function CompareName(ByV al X As Person, _
    ByVal Y As Person) As Boolean
    If x.Name > y.Name Then
    Return True
    Else
    Return False
    End If
    End Function

    Function CompareAge(ByVa l X As Person, _
    ByVal Y As Person) As Boolean
    If x.Age > y.Age Then
    Return True
    Else
    Return False
    End If
    End Function

    Or even a complex comparison that compares multiple properties.
    Function CompareAddress( ByVal X As Person, _
    ByVal Y As Person) As Boolean
    If x.State > y.State Then
    Return True
    ElseIf x.State < y.State Then
    Return False
    ElseIf x.City > y.City Then
    Return True
    ElseIf x.City < y.City Then
    Return False
    ElseIf x.Street > y.Street Then
    Return True
    ElseIf x.Street < y.Street Then
    Return False
    Else
    Return False
    End If
    End Function

    Dim people() As Person

    Sort.SelectionS ort(AddressOf Sort.CompareNam e, people)
    Sort.SelectionS ort(AddressOf Sort.CompareAge , people)
    Sort.SelectionS ort(AddressOf Sort.CompareAdd ress, people)

    Note the "standard" convention is for Compare to return an Integer that is
    either less, greater or equal to 0. To indicate if the parameters are less,
    greater or equal to each other.

    --
    Hope this helps
    Jay [MVP - Outlook]
    ..NET Application Architect, Enthusiast, & Evangelist
    T.S. Bradley - http://www.tsbradley.net


    "Lore Leunoeg" <loreleunoeg@gm x.net> wrote in message
    news:dkljla$3m9 $1@news01.versa tel.de...
    | Hello Jay
    | Thank you.
    | But if the Invoke method is like calling the method directly. Why does
    this
    | example use delegates? Do you have got an idea?
    |
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 APR.1033/vblr7/html/vastmDelegate.h tm
    | Sincerely
    | Lore
    | --- example: ---
    | Public Class SortClass
    | Delegate Function Compare(ByVal x As Integer, _
    | ByVal y As Integer) As Boolean
    |
    | Function CompareValues(B yVal X As Integer, _
    | ByVal Y As Integer) As Boolean
    | If X > Y Then
    | CompareValues = True
    | Else
    | CompareValues = False
    | End If
    | End Function
    |
    | Sub SelectionSort(B yVal IsGreaterThan As Compare, _
    | ByVal IntArray() As Integer)
    | Dim MaxVal As Integer
    | Dim MaxIndex As Integer
    | Dim i, j As Integer
    |
    | ' Step through the elements in the array starting with the
    | ' last element in the array.
    | For i = UBound(IntArray ) To 1 Step -1
    | MaxVal = IntArray(i)
    | MaxIndex = i
    | For j = 1 To i
    | ' Use the delegate to compare values.
    | If IsGreaterThan.I nvoke(IntArray( j), MaxVal) Then
    | MaxVal = IntArray(j)
    | MaxIndex = j
    | End If
    | Next j
    | ' Use the delegate to compare values.
    | If IsGreaterThan.I nvoke(i, MaxIndex) Then
    | IntArray(MaxInd ex) = IntArray(i)
    | IntArray(i) = MaxVal
    | End If
    | Next i
    | End Sub
    | End Class
    |
    | Class Class1
    | Sub SortArray()
    | Dim Sort As New SortClass()
    | Dim arr1() As Integer = {1, 5, 3, 2, 7, 22, 5, 54, 12}
    | Sort.SelectionS ort(AddressOf Sort.CompareVal ues, arr1)
    | MsgBox("Array sorted.")
    | End Sub
    | End Class
    |
    | ' Add a button to your main form and insert the following code
    | ' into the Click event handlr.
    | Private Sub Button1_Click(B yVal sender As System.Object, _
    | ByVal e As System.EventArg s) _
    | Handles Button1.Click
    | Dim c As New Class1()
    | c.SortArray()
    | End Sub
    | --- end example ---
    |
    <<snip>>


    Comment

    Working...