-1

I'm trying to run a compression with a backgroudworker and it's running fine. The Compressor-SDK is giving me a function for calling Status-Callbacks, which works fine too, but without a backgroundworker. When I try to call the function inside the backgroundworker, the backgroundworker tells me it could only run as one thread, then the process is canceled.

How do I call this function inside the BW?

The function I'm trying to call in the backgroundworker:

Public Function SqxCallback(ByVal pParam As IntPtr, ByRef CallbackInfo As SQX_CALLBACKINFO) As Integer

    If CallbackInfo.pszSourceFileName IsNot Nothing Then
        Me.lblStatusMsg.Text = " compressing... " & CallbackInfo.pszSourceFileName
        Me.lblStatusMsg.Refresh()
    End If


    Me.ProgressSingle.Value = CallbackInfo.iProgress

    Return 1

End Function
Bailee
  • 1
  • 2
  • possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Bjørn-Roger Kringsjå Jul 30 '15 at 14:42
  • and [How to update GUI with backgroundworker?](http://stackoverflow.com/questions/1862590/how-to-update-gui-with-backgroundworker) – Bjørn-Roger Kringsjå Jul 30 '15 at 14:50
  • There are a few issues here, but one that will get you is trying to change the text on `Me.lblStatusMsg` as you can't inside the background worker **unless using delegates and the control needs to be invoked** and such... Also instead of passing the `CallbackInfo` to the function why don't you just check the `pszSourceFileName` before sending it: you can pass a boolean and check that boolean inside your function. Besides you are not doing anything with that object besides setting a progress bar value it looks like. You can set this after the function runs... Just a few things to think about – Trevor Jul 30 '15 at 15:51

1 Answers1

0
Private BGWorker As BackgroundWorker

BGWorker = New BackgroundWorker()
BGWorker.WorkerReportsProgress = True
BGWorker.WorkerSupportsCancellation = True

AddHandler BGWorker.ProgressChanged, New ProgressChangedEventHandler(AddressOf BGWorker_ProgressChanged)


Private Sub BGWorker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)

    Try
        If e.UserState.ToString <> "" Then
            Me.lblStatusMsg.Text = e.UserState.ToString
            Me.lblStatusMsg.Refresh()
        End If

        Me.ProgressSingle.Value = e.ProgressPercentage

    Catch ex As Exception
        'We don't care
    End Try

End Sub

Public Function SqxCallback(ByVal pParam As IntPtr, ByRef CallbackInfo As SQX_CALLBACKINFO) As Integer
    Dim message as String = ""
    If CallbackInfo.pszSourceFileName IsNot Nothing Then
        message = " compressing... " & CallbackInfo.pszSourceFileName
    End If

    BGWorker.ReportProgress(CallbackInfo.iProgress, Message & "...")

    Return 1

End Function
EJD
  • 751
  • 6
  • 19