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, i stop the background thread, run a function, but i cant get the background thread to start running again (i get an error saying the back ground worker is busy, which i dont get since i stopped the thread first. heres the code i got:
Code:Private Sub BackgroundWorker1_DoWork( _ ByVal sender As Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork Do Until IsItCanceled = True Dim worker As System.ComponentModel.BackgroundWorker worker = CType(sender, System.ComponentModel.BackgroundWorker) Dim WC As Class2 = CType(e.Argument, Class2) WC.FindMyPort(worker, e) Loop End SubCode:Private Sub BackgroundWorker1_ProgressChanged( _ ByVal sender As Object, _ ByVal e As System.ComponentModel.ProgressChangedEventArgs) _ Handles BackgroundWorker1.ProgressChanged Dim state As Class2.CurrentState = _ CType(e.UserState, Class2.CurrentState) Me.Label10.Text = "d" End Subclass2:Code:Private Sub BackgroundWorker1_RunWorkerCompleted( _ ByVal sender As Object, _ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BackgroundWorker1.RunWorkerCompleted If e.Error IsNot Nothing Then MsgBox("Error: " & e.Error.Message) BackgroundWorker1.Dispose() ElseIf e.Cancelled Then MsgBox("Word counting canceled.") BackgroundWorker1.Dispose() Else BackgroundWorker1.Dispose() End If End Sub
when the user click a button, i run this (after a bunch of other stuff)Code:Option Compare Text Imports System.Windows.Forms Imports System.Data.OleDb Imports System Imports System.Data Imports System.Data.SqlClient Imports GsmComm.GsmCommunication Imports GsmComm.PduConverter Imports System.IO.Ports Imports System.IO.Ports.SerialPort Imports System.Text Imports System.IO Public Class Class2 Public Class CurrentState Public LinesCounted As Integer Public WordsMatched As Integer End Class Public SourceFile As String Public CompareString As String Private WordCount As Integer = 0 Private LinesCounted As Integer = 0 Public isAmodem As Boolean = False Public Sub ShowMessage(ByRef MsgPDU As SmsDeliverPdu) Try Dim txtMsg As String = MsgPDU.UserDataText Dim FromNumber As String = MsgPDU.OriginatingAddress Dim FromAddress As String = MsgPDU.OriginatingAddress Dim TimeStamp As String = MsgPDU.GetTimestamp.ToString Dim MsgFromName As String = FindName(FromNumber) Dim AddString As String = "Insert into tblInboxMsg (MsgStatus,MsgFromNum,MsgFromName,TxtMsg,MsgTimeStamp) values" _ & " ('New','" & FromAddress & "','" & MsgFromName & "','" & txtMsg & "','" & TimeStamp & "')" Debug.Print(AddString) ' Dim FileName As String = CurDir() & "\SMSer.mdb" Dim conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\JasonWucinski.mdb;Persist Security Info=True;Jet OLEDB:Database Password=") Dim Cmd As OleDbCommand Dim objCmd As New OleDbCommand Cmd = New OleDbCommand(AddString, conn) conn.Open() objCmd = New OleDbCommand(AddString, conn) objCmd.ExecuteNonQuery() conn.Close() 'JasonWucinskiBots5 If Not (Cmd Is Nothing) Then Cmd.Dispose() Cmd = Nothing If Not (objCmd Is Nothing) Then objCmd.Dispose() objCmd = Nothing Catch ex As Exception Debug.Print("ex.ToString") Debug.Print(ex.ToString) End Try End Sub Public Sub FindMyPort(ByVal worker As System.ComponentModel.BackgroundWorker, _ ByVal e As System.ComponentModel.DoWorkEventArgs) If worker.CancellationPending Then e.Cancel = True Exit Sub Else Dim getState As Boolean = False Dim getManu As String Dim storage = getMessageStorage() Dim MsgLocation As Integer Dim counter As Integer = 0 Try With comm Try .Open() Dim info As IdentificationInfo = comm.IdentifyDevice getManu = info.Manufacturer If getManu = "huawei" Then Try Dim messages As DecodedShortMessage() = comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, PhoneStorageType.Sim) For Each DecodedShortMessage In messages MsgLocation = DecodedShortMessage.Index ShowMessage(DecodedShortMessage.Data) counter = counter + 1 Next Catch ex As IOException End Try If counter >= 1 Then Try MsgBox("You got a new message") Catch ex As Exception End Try End If comm.Close() Exit Try End If Catch Ex As IOException Catch Ex As Exception End Try End With Catch ex As Exception End Try End If Threading.Thread.Sleep(1000) End Sub End Class
but all i get is an error saying the thread is bussy and cant have duplicatesCode:Dim WC As New Class2 IsItCanceled = False Me.BackgroundWorker1.RunWorkerAsync(WC)




Reply With Quote