|
-
Dec 16th, 2002, 12:36 PM
#1
Thread Starter
Sleep mode
type-safe function
"Delegates are type-safe Function Pointers or Callbacks"
what the hell does that mean?It seems to be boring issue but I have to know coz it deals with event and handlers.
thanx in advance.
-
Dec 16th, 2002, 12:50 PM
#2
A delegate outlines the specific parameters of the method, the method signature:
Public Delegate Sub OnClose(sender as object, e as ClosingEventArgs)
These means that only a function that has that same signature can be passed in as the parameter. Unlike in in some cases with C++ or VB6 (I think) and the AddressOf method that could be passed in any method signature even though it would fail if not correct. These would be more generic callbacks.
-
Dec 16th, 2002, 01:20 PM
#3
Thread Starter
Sleep mode
I would be grateful if you passed me a very easy example of that.
thanx for your reply Edneeis.
-
Dec 16th, 2002, 03:18 PM
#4
Well here is an example, its not the easiest but since callbacks aren't used a lot i think this is easy enough. I have some threading in there just to have a reason for a callback.
VB Code:
'FORM CODE STARTS HERE NEEDS ONE BUTTON
Public Sub WhenDone(ByVal final As ThreadedHandler)
'since this will be passed through the delegate you ensure that it is type safe
'and know that the right object will be passed
MsgBox("I'm finished here is the button text:" & ControlChars.NewLine & final.ID)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'the constructor sets the callback and wait time
Dim test As New ThreadedHandler(AddressOf WhenDone, 750)
'run the method
test.Execute()
End Sub
'CLASS CODE STARTS HERE
Public Class ThreadedHandler
'delegate with type safe parameters
Public Delegate Sub OnDone(ByVal final As ThreadedHandler)
Private _cb As OnDone
Private _id As Guid
Private _ms As Integer
'provides a means fo identifying the same object
Public ReadOnly Property ID() As String
Get
Return _id.ToString
End Get
End Property
'starts a threaded wait method
Public Sub Execute()
Dim t As New Threading.Thread(AddressOf Wait)
t.Start()
End Sub
'this will wait and when done make a callback
'should be started on a new thread
Private Sub Wait()
Dim curThread As Threading.Thread
curThread.CurrentThread.Sleep(_ms)
_cb.Invoke(Me)
End Sub
'make new id and pass a callback and the wait time
Public Sub New(ByVal callback As OnDone, ByVal interval As Integer)
_cb = callback
_id = Guid.NewGuid
_ms = interval
End Sub
End Class
-
Dec 16th, 2002, 04:20 PM
#5
Thread Starter
Sleep mode
Edneeis I owe you .You're such great man .
I will try it now.
Thank you soooo much
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|