background worker no reaction

dadibarca23

New member
Joined
Apr 12, 2016
Messages
2
Programming Experience
1-3
hey everyone. can anyone tell me how to call a function from a secondary thread. in my program, i have a background worker running. when the user clicks a button on the main form
Does not make any reaction and Thanks
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork
        soldDJEZZY()
    End Sub
    Private Sub BGW_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGW.ProgressChanged
        TextBox5.Text = e.UserState.ToString
    End Sub
    'buton solde djezzy
    Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
      
        If Not BGW.IsBusy Then BGW.RunWorkerAsync()


    End Sub
    Sub soldDJEZZY()


        strCommand = String.Format("AT+CUSD=1," & Chr(34) & "{0}" & Chr(34) & ",15", "*" & parrametre.TextBox9.Text & "#")
        Dim strName As String = parrametre.cbxDevices2.Text()
        Dim iDeviceSpeed As Integer
        If (Not Integer.TryParse(parrametre.cbxDeviceSpeed2.Text, iDeviceSpeed)) Then
            iDeviceSpeed = 0
        End If
        objGsm.Open(strName, "0000", iDeviceSpeed)
        If (objGsm.LastError <> 0) Then
            If (objGsm.LastError = 36103) Then
                MessageBox.Show("Invalid Pin entered: SIM card can be blocked after a number of false attempts in a row.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If


        ' Sends the USSD Command though the selected GSM Modem




        If (objGsm.LastError = 0) Then
            objGsm.SendCommand(strCommand)
        End If
        ' Reads the response from the GSM Modem
        If (objGsm.LastError = 0) Then
            strResponse = objGsm.ReadResponse(10000)
        End If


        If (objGsm.LastError = 0) Then


            If (strResponse.Contains("OK")) Then ' Response should be OK
                objGsm.SendCommand(String.Empty)
                strResponse = objGsm.ReadResponse(10000)


                If (objGsm.LastError <> 0) Then
                    UpdateResult2(objGsm.LastError)
                    Return
                End If


                If (strResponse.Contains("+CUSD:")) Then


                    strFields = strResponse.Split(Char.Parse(Chr(34)))


                    If (strFields.Length > 1) Then
                        strResponse = strFields(1)
                    Else
                        strResponse = strFields(0)
                    End If
                End If
            End If
        End If
        BGW.ReportProgress(0, strResponse)
        TextBox5.Text = (strResponse)
        UpdateResult2(objGsm.LastError)
        objGsm.Close()
        'Cursor = Cursors.Default


    End Sub
 
Last edited by a moderator:
TextBox5.Text = (strResponse)
Remove this from your DoWork handler, you don't access form control directly from that background thread.
You also get lots of text values from what looks like controls in DoWork, don't do that, instead pass them in to RunWorkerAsync. You must pass a single object, this can be an array or list or any object where you set the different properties.

You are passing strResponse to ReportProgress. Also to report progress you must enable WorkerSupportsProgress, select the BackgroundWorker component in designer and turn on that in Properties window. If the background work only produces a single result then you don't need progress reporting, you would instead set e.Result in DoWork and handle RunWorkerCompleted event, where you also get the result from e.Result.

Regarding "UpdateResult2" call in DoWork, beware that this also is called on secondary thread and that method also can't access controls directly.
 
hi who are you

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork

soldDJEZZY()
End Sub

Private Sub BGW_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGW.ProgressChanged
TextBox5.Text = e.UserState.ToString
End Sub

'buton solde djezzy
Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click

If Not BGW.IsBusy Then BGW.RunWorkerAsync()

strCommand = String.Format("AT+CUSD=1," & Chr(34) & "{0}" & Chr(34) & ",15", "*" & parrametre.TextBox9.Text & "#")
Dim strName As String = parrametre.cbxDevices2.Text()
Dim iDeviceSpeed As Integer
If (Not Integer.TryParse(parrametre.cbxDeviceSpeed2.Text, iDeviceSpeed)) Then
iDeviceSpeed = 0
End If
objGsm.Open(strName, "0000", iDeviceSpeed)
If (objGsm.LastError <> 0) Then
If (objGsm.LastError = 36103) Then
MessageBox.Show("Invalid Pin entered: SIM card can be blocked after a number of false attempts in a row.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If

End Sub

Sub soldDJEZZY()

' Sends the USSD Command though the selected GSM Modem


If (objGsm.LastError = 0) Then
objGsm.SendCommand(strCommand)
End If
' Reads the response from the GSM Modem
If (objGsm.LastError = 0) Then
strResponse = objGsm.ReadResponse(10000)
End If

If (objGsm.LastError = 0) Then

If (strResponse.Contains("OK")) Then ' Response should be OK
objGsm.SendCommand(String.Empty)
strResponse = objGsm.ReadResponse(10000)

If (objGsm.LastError <> 0) Then
UpdateResult2(objGsm.LastError)
Return
End If

If (strResponse.Contains("+CUSD:")) Then

strFields = strResponse.Split(Char.Parse(Chr(34)))

If (strFields.Length > 1) Then
strResponse = strFields(1)
Else
strResponse = strFields(0)
End If
End If
End If
End If
BGW.ReportProgress(0, strResponse)
MsgBox(strResponse)
UpdateResult2(objGsm.LastError)
objGsm.Close()
'Cursor = Cursors.Default

End Sub

Private Sub BGW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
If e.Cancelled = True Then
MsgBox("Canceled!")
ElseIf e.Error IsNot Nothing Then
MsgBox("Error: " & e.Error.Message)
Else
MsgBox("Done!")
End If
End Sub
can you help me plea
 
Back
Top