473,419 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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
Nov 23 '05 #1
1 1951
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.SelectionSort(AddressOf Sort.CompareLessThan, 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(ByVal comparison As Compare, _
| ByVal people() As Person)

Function CompareName(ByVal 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(ByVal 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.SelectionSort(AddressOf Sort.CompareName, people)
Sort.SelectionSort(AddressOf Sort.CompareAge, people)
Sort.SelectionSort(AddressOf Sort.CompareAddress, 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" <lo*********@gmx.net> wrote in message
news:dk**********@news01.versatel.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.2003APR.1033/vblr7/html/vastmDelegate.htm
| Sincerely
| Lore
| --- example: ---
| Public Class SortClass
| Delegate Function Compare(ByVal x As Integer, _
| ByVal y As Integer) As Boolean
|
| Function CompareValues(ByVal X As Integer, _
| ByVal Y As Integer) As Boolean
| If X > Y Then
| CompareValues = True
| Else
| CompareValues = False
| End If
| End Function
|
| Sub SelectionSort(ByVal 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.Invoke(IntArray(j), MaxVal) Then
| MaxVal = IntArray(j)
| MaxIndex = j
| End If
| Next j
| ' Use the delegate to compare values.
| If IsGreaterThan.Invoke(i, MaxIndex) Then
| IntArray(MaxIndex) = 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.SelectionSort(AddressOf Sort.CompareValues, 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(ByVal sender As System.Object, _
| ByVal e As System.EventArgs) _
| Handles Button1.Click
| Dim c As New Class1()
| c.SortArray()
| End Sub
| --- end example ---
|
<<snip>>
Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Nick | last post by:
Hi all, Are the two snippets below equivelent, since they both only continue once the ShowForm method returns??? 1. delShowForm del = new delShowForm(ShowForm); //show form shows a form...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
2
by: Stephen Lamb | last post by:
What type(s) of exceptions will be thrown when calling Form.Invoke when the handle for the form has yet to be created? MS only states, "If no appropriate handle can be found, the Invoke method...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
6
by: Valerie Hough | last post by:
I'm not entirely sure what the difference is between these two approaches. In order to avoid reentrant code, I was using Control.BeginInvoke in my UI to cause an asynchronous activity to be done...
3
by: m.posseth | last post by:
Hello does someone know how i can invoke a method in the underlying thread without the usage of a window handle ?? This works perfect in a form Me.Invoke(New MethodInvoker(AddressOf...
4
by: LamSoft | last post by:
It seems that my program is dead lock after running this sentence (bold) private delegate void updateBuildingDetailCallBack(String key, String Value); private void updateBuildingDetail(String...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.