[VB.NET] Redémarrage d'un Thread
Bonjour,
J'écris une petite application qui fait la chose suivante :
Lecture et Log d'information recu sur le port série 1.
Lorsqu'une certaine string est lue vie ce port série 1, l'application doit ouvrir la com2 et faire un appel via un modem.
Afin de ne pas bloquer la réception de la com1, j'utilise un thread pour lancer la procédure d'appel COM2.
Jusque la tout fonctionne bien.
Le problème se pause lorsque plus tard une deuxième string entre sur le port série 1, je ne peux pas relancer mon thread. (voir le code plus bas)
Comment pourrai-je solutionner mon problème ?
Merci d'avance,
Looney
Un peu de code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
Private WithEvents moRS232 As Rs232
Private WithEvents moCOM3 As Rs232
Private p_CallTech As Thread
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
p_CallTech = New Thread(New ThreadStart(AddressOf CallTech))
End Sub
'.... Initiation de la COM2, ...
Private Sub Com1_EventRaised(ByVal source As Rs232, ByVal mask As Rs232.EventMasks) Handles moRS232.CommEvent
Debug.Assert(Me.InvokeRequired = False)
RxPanel.Visible = True
Dim iPnt As Int32, sBuf As String, Buffer() As Byte
Debug.Assert(Me.InvokeRequired = False)
If (mask And Rs232.EventMasks.RxChar) > 0 Then
Buffer = source.InputStream
For iPnt = 0 To Buffer.Length - 1
If Buffer(iPnt) <> 13 Then
sOutput += Chr(Buffer(iPnt))
Else
If Not sOutput Is Nothing Then
If sOutput.IndexOf("Cause Code=16") <> -1 Then
If(p_CallTech.IsAlive = False) then
p_CallTech.Start() ' Mon thread démarre ici
End If
sOutput = Nothing
End If
Else
sOutput = Nothing
End If
End If
If (ckShow.Checked) Then
tbDataCom1.AppendText(Chr(Buffer(iPnt)))
End If
If tbDataCom1.TextLength > 59950 Then
tbDataCom1.Text = ""
End If
Next
End If
RxPanel.Visible = False
End Sub
Private Sub CallTech()
RxPanel2.Visible = True
moCOM3 = New Rs232
Try
'// Setup parameters
With moCOM3
.Port = 3
.BaudRate = 9600
.DataBit = 8
.StopBit = Rs232.DataStopBit.StopBit_1
.Parity = Rs232.DataParity.Parity_None
.Timeout = 300
End With
'// Initializes port
tbDataCom2.AppendText("Initializing COM3 ..." + vbCrLf)
moCOM3.Open()
'// Set state of RTS / DTS
tbDataCom2.AppendText("Enabling DTR & RTS" + vbCrLf)
moCOM3.Dtr = True
moCOM3.Rts = True
tbDataCom2.AppendText("Enabling Events" + vbCrLf)
moCOM3.EnableEvents()
'btnConnectCom1.Enabled = False
'btnDisconnect.Enabled = True
Dim sTx As String
'----------------------
'// Clear Tx/Rx Buffers
tbDataCom2.AppendText("Purging Buffers Tx - Rx" + vbCrLf)
moCOM3.PurgeBuffer(moCOM3.PurgeBuffers.TxClear Or moCOM3.PurgeBuffers.RXClear)
sTx = "ATDTxxxxxxxxxxxxxxxxxxx"
tbDataCom2.AppendText("Dialing with String : " & sTx & " " + vbCrLf)
sTx += ControlChars.Cr
moCOM3.Write(sTx)
tbDataCom2.AppendText("Thread Sleeping 25sec..." + vbCrLf)
System.Threading.Thread.Sleep(25000)
Application.DoEvents()
tbDataCom2.AppendText("Closing COM3" + vbCrLf)
moCOM3.Close()
RxPanel2.Visible = False
Catch Ex As Exception
MessageBox.Show(Ex.Message, "Connection Error", MessageBoxButtons.OK)
End Try
End Sub |