Question : Raising events with asynchronous callback from a dll

This is a VB.Net Question
OK, so I've figured (with some help) out how to receive an asynchronous callback from a dll written in C++ using delegates.  But I've also just figured out that a) callbacks run on separate threads from the UI and b) I don't -need/want- a separate thread.  What I'd like to do is have the callback raise an event that interrupts rather than 'parallel' (a separate thread) the main thread (if possible).
So this is a three part question:
1)  Is it possible to for a callback to raise an event in the primary thread?
2) If this isn't possible, is there any way to use events for this purpose or should I simply resign myself to handling a separate thread for each callbacks?
3) Any code examples of how to do either would be great, thanks
pgb

Answer : Raising events with asynchronous callback from a dll

Invoke or BeginInvoke functions can help you to call form functions from callback.
For example:

Delegate Sub CallbackDelegate(ByVal n1 As Integer, ByVal n2 As Integer)

Public Class MainForm Inherits System.Windows.Forms.Form

        ' this is callback running in the context of caller thread
        Private Sub CallbackFunction(ByVal n1 As Integer, ByVal n2 As Integer)
              Me.BeginInvoke( _
                  New  CallbackDelegate(AddressOf Me.HandleData), _
                  New Object() {n1, n2})
        End Sub

        ' This function runs in the main application thread and called indirectly from CallbackFunction
        Private Sub HandleData(ByVal n1 As Integer, ByVal n2 As Integer)
             ...
        End Sub

        ...

End Class
Random Solutions  
 
programming4us programming4us