Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I cannot get this part
VB
Delegate Function WaveInfoHandler(ByVal lLastx As Integer, ByVal lLastLeftY As Integer, ByVal lLastRightY As Integer, _ ByVal lx As Integer, ByVal ly As Integer, ByVal lEnd As Integer, ByVal lAbsSizeY As Integer, ByVal bRectangles As Boolean, ByVal lMidy As Integer) As WaveData 

Delegate Sub UpdateUIHandler(ByVal Statusmsg As String, ByVal result As WaveData) 

Private WaveInfo_handler As WaveInfoHandler = AddressOf WaveInfo 


:confused:
VB
Private WaveInfo_callback As AsyncCallback = AddressOf GetWaveInfo_callback 
:confused:
VB
Private Function WaveInfo(ByVal lLastx As Integer, ByVal lLastLeftY As Integer, ByVal lLastRightY As Integer, _ ByVal lx As Integer, ByVal ly As Integer, ByVal lEnd As Integer, ByVal lAbsSizeY As Integer, _ ByVal bRectangles As Boolean, ByVal lMidy As Integer) As WaveData
Posted
Updated 3-Oct-10 0:54am
v2
Comments
Henry Minute 3-Oct-10 6:55am    
You cannot get this part of what? Where does this code come from? What part don't you get? So many questions, so little time.
Sandeep Mewara 3-Oct-10 7:40am    
Sorry but we arn't magicians! You need to tell what and where from about the code to get some help.
Sandeep Mewara 3-Oct-10 7:41am    
And yes - Keeping your question title a little specific always help. All CAPS are considered shouting so avoid it.

It is actually kind of simple. You need to understand the three actual parts that are necessary for using delegates. Also, you must understand that delegates are used to call functions of which the actual code isn't known at time of writing. A good example is an event, that is also a delegate type. If you have a button and create a click event handler for it, this is a delegate that you are filling with your specific code.

The three parts that used:
- delegate definition

For a button the definition stated that the functions first parameter is Object sender and the second is e as EventArgs. This definition actually has the name CommandEventHandler. In your question the first two with the keyword delegate are also the actual definitions. The name of the first is for example WaveInfoHandler. This may look a bit confusing, because it looks almost like a normal function definition, but in this case the name WaveInfoHandler is the actual type name instead of the function name. The delegate keyword changes the way of interpretation from "a function with the name WaveInfoHandler" to "a function definition with the name WaveInfoHandler".

- The placeholder

This is simply a variable definition that can hold the address of a function that has the same layout as defined earlier. The parameters and return type of such a function must fit exactly, but the name of the function itself is free to name it whatever you like. WaveInfo_handler is such a placeholder defined in your question.

- The actual function implementation

The actual implementation can be a function created by you. As told earlier, the function implementation must have the same signature of the WaveInfoHandler definition in order to fit it into the placeholder. As you can see, the WaveInfo function is almost exacly the same as the delegate definition of WaveInfoHandler except for the name. As you now know the name is not relevant here, making it possible to fill the placeholder WaveInfo_handler with the address of that function implementation.
Private WaveInfo_handler As WaveInfoHandler = AddressOf WaveInfo 


You can have multiple implementations and assign a different function address whenever you like. This means that the assigned function address at that time is called. The code that is calling this function implementation didn't have to know the actual function at the time of writing. This is making it a very powerful type and extremely useful.

Good luck!
 
Share this answer
 
Comments
Abhinav S 3-Oct-10 11:21am    
Good answer.
Extra information about Invoke


I would expect you would have added the comment you placed as answer about invoke. But here's the answer anyway :-)
The reason this is posted as another answer is because I got an error when trying to add it as a comment (I think because of the size of it)



When a thread wants to update the user interface, this cannot be done directly. The main reason for that, is that the UI is event driven. Meaning that there must be some action to get any reaction. The main program loop that is responsible for the UI (repainting, handling button clicks, etc...) waits for a message by repeated calls to the OS. This is a so called message loop (http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx[^]) that helps receiving input in an efficient and generic way. This also a reason why you should use threads for intensive processing because the OS will report your application as "not responding" if you do not occasionally handle these messages.

Well, this is some information aside but actual one of the main reasons why invoke is used for handling UI updates. When you would update the UI from another thread than the main thread, the actual UI would not be updated because there was no request to do this and no actual repaint event was fired. It also would be possible that multiple threads besides the main thread are doing screen updates without any synchronization what so ever. This is not excepted and will throw an exception. To overcome this, the actual change should be made in the "context" of the main thread (You could say that "context" is the workplace of a certain thread). This means that a mechanism must be used, in such a way that the actual code you would like to be executed, get transfered to the main thread and let that thread execute it. Because the main thread is caught up in handling incoming messages, the most easy way is to simply pass it a message which makes it clear that we would like it to execute certain code. To minimize the size of the message and overhead we don't send it the code we would like to execute, but use an powerful trick that was just discussed earlier... as you might already guess "delegates". A thread, other than the UI thread, will send a message with the request to execute a certain function, that was passed as delegate (function address) with some parameters that must be passed to that function. This messages is picked up by the main thread and then executed, which handles only one message at a time, so even if there are a lot of threads that are all sending an occasional message to the main thread, these are each handled one by one, providing a very nice method of synchronization.

Some extra info: In .net you will often encounter a check to see if invoke is actually necessary. This is done by a check using InvokeRequired. This is of course to prevent it from invoking over and over again. Most threads that update the UI have a specific function for this that will check if invoke is required and invoke themselves if this is needed. The function will then be called by the main thread and the outcome of the check to see if invoke is required will be false. The necessary screen updating can then be done safely. This way the function is providing screen updating and invocation when necessary all by itself. Of course the check to see if invoke is required is simply to compare the current thread (obtained by GetCurrentThread) and the main window thread (obtained by GetWindowThreadProcessID) and if these are not equal an invoke is required.

There is also another type of invocation called Pinvoke. For more information on that I would recommend checking out the following link:
Using Platform Invoke[^]

Hopefully this gave you a good idea about invoke.
 
Share this answer
 
v4
Comments
Sandeep Mewara 4-Oct-10 12:20pm    
Comment from OP:
Thanks a lot E.F.Nijboer For your valuable Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900