write delegate & callback in dll

J

James Wong

hi,

I want to write some delegate function to the DLL.
How can I output the value to my main program? (Do not output the value to
the UI)
Any good idea for this program?

Thanks~
For example,

Public Class clsCallBackAdd
Delegate Function _AddDelegate(ByVal intInteger As Integer) As Integer

Private Function _Add(ByVal intInteger As Integer) As Integer
intInteger += 1
Return intInteger
End Function

Public Sub gSetDelegate(ByVal intInteger As Integer)
Dim deleg As _AddDelegate
Dim cb As New AsyncCallback(AddressOf gCallBack)

deleg = AddressOf _Add
deleg.BeginInvoke(intInteger, cb, deleg)
End Sub

Public Sub gCallBack(ByVal ar As IAsyncResult)
......
End Sub
End Class
 
K

Kevin Yu [MSFT]

Sorry, James. I didn't quite catch your meaning. What do you mean by output
the value to your main program? Do you mean the return value in the _Add
function? Could you explain a little more detail? Thank you!

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

James Wong

I have a windows form (eg. Public Class Form1) will call this DLL,
this form will pass some parameter to the DLL,
When the DLL is processing, my Form1 will do another process.
After the DLL finished the process, how can I get the result value?
 
G

guy

James said:
I have a windows form (eg. Public Class Form1) will call this DLL,
this form will pass some parameter to the DLL,
When the DLL is processing, my Form1 will do another process.
After the DLL finished the process, how can I get the result value?

You rise event in dll and catch it on main form.
 
K

Kevin Yu [MSFT]

Hi James,

So you mean you need to return the value from _Add function that is
calculated on another thread to the main thread. Am I right?

To return the value, the simplest way is to set the value to a certain
variable in the _Add function instead of returning a value. And in the
callback, make the main thread read from that variable. By the way, when
setting the variable value, we have to use SyncLock statement to lock the
object to make sure that no other thread is writing to it simultanously.

For more information about SyncLock, please check the following link:

http://msdn2.microsoft.com/en-us/library/3a86s51t.aspx

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top