fabulous Level: VB Guru

 Registered: 03-08-2002 Posts: 439
|
Re: what is callback function
In .NET, a callback function is much like an event or a delegate. It serves a similar purpose in that it is called when a particular process has been performed and you have to write the code for it. Basically, you create an object and send it to do something, usually in another thread or process. When the object has done what you have sent it to, it will need a way to notify you. It then calls the callback you would have used to create it. I believe that the callback is run on the same thread the worker object is in.
The signature of the callback varies depending on the object you are using. For instance, the System.Threading.Timer uses the call back mechanism to notify you at each elapsed interval. The requirement is that your callback be a Sub (void in C#) and have a single parameter of type Object which is passed ByVal. Here is an example of the code.
'the callback
Private Sub MyCallBack(ByVal state As Object)
'whatever you want to do when the callback is called
End Sub |
This is how you set up the timer to use the callback
Dim _tmr As System.Threading.Timer
_tmr = New System.Threading.Timer(New TimerCallback(AddressOf MyCallBack), Nothing, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)) |
The above code creates an instance of the timer class that will call the callback after 5 seconds and then at 1 second intervals afterwards.
The first parameter is the address of the callback
The second parameter can be anything you want to use in the call back. This is what will be put in the callback as the state parameter.
The 3rd parameter determines how long the timer will wait before raising the first event
The 4th parameter determines the interval between subsequent calls to the callback procedure.
Hope this helps. Happy coding.
____________________________
My boss is a Jewish Carpenter (Jesus Christ)

Brain Bench Certified VB.NET Developer
|