Delegates

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I've only ever used delegates before when trying to update a ProgressBar etc.
Unfortunately, I now have to write an application that communicates with a third party DLL written by a coder who has no knowledge of VB.

Here's what I have so far:-

VB.NET:
    Public Declare Function Optimise Lib "nameremoved.dll" (ByVal hPtr As IntPtr, ByVal lpDir As String, _
    ByVal lpJob As String, ByVal lpSystem As String, ByVal jFlags As Long, ByVal OCB As OptimiserCallback) As Long

    Private Delegate Function OptimiserCallback(ByVal sMode As Integer, ByVal lParam As String) As Long
    Private cb As OptimiserCallback

    Private JOBDirectory As String = "f:\"
    Private JOBFile As String = "test"

    Private Result As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        cb = AddressOf OptimiserCallbackFunction
        Result = Optimise(Me.Handle, JOBDirectory, JOBFile, "c:\", 0, cb)
    End Sub

    Private Function OptimiserCallbackFunction(ByVal sMode As Integer, ByVal lParam As String) As Long
        OptimiserCallbackFunction = 0 'return 2 to cancel
        Select Case sMode
            Case 10
                'better solution found
                'TODO: update screen
            Case 12
                'success
                Button1.Enabled = True
            Case 13
                'abnormal termination
                Button1.Enabled = True
        End Select
    End Function

The call to the DLL works, and it performs the function it is supposed to, but I dont appear to be getting any feedback via the delegate.

Any suggestions would be greatly appreciated :)
 
Still havent managed to sort this - any suggestions :confused:
 
Hi

In the callback methode you need something like this
VB.NET:
'Check the current used thread of the form. If nessecary, we have to switch to the Forms thread.
If me.InvokeRequired = True Then

  'Call now the same methode but with the delegate. Perhaps you need to declare your delegate because of own used parameters
  me.invoke(New Eventhandler(addressof Button1_Click), sender, e)

  'Because we are now in the wrong thread, just exit. The delegate will work now for you. 
  exit sub

End If
 
Last edited:
Back
Top