Hello everybody, I need help replacing a do..loop..until loop. I use the loop to login and out of an email inbox to check for a fax server response. I know I can easily replace it with a timer but the problem is that the loop resides in a thread. Im a little uneasy about putting a timer in a thread since there will be mutltiple threads running simultaneously. Does anybody have any suggestions on replacing the loop?? Or has anybody ever had timers in a multithreaded environment and lived to tell about it?
Heres a sample of my code:
Heres a sample of my code:
Code:
Public Sub SendFaxResponse()
Try
Dim counter As Integer
Dim moveOn As Boolean = False
Dim loopPass As Date = Now() 'used to prevent an infinite loop
Do
'Check the inbox for response
Dim strServer As String = aServer
Dim strUser As String = aAccountName
Dim strPass As String = aPassword
Dim objpop As qqMail.qqPop = New qqMail.qqPop
objpop.PopServer = strServer
objpop.PopUser = strUser
objpop.PopPass = strPass
'Must repeatly login and out to refresh inbox
objpop.Login()
Dim MsgId As Integer = objpop.MaxMsgNr
If MsgId > 0 Then
For counter = MsgId To 1 Step -1
Dim objMail As qqMail.MailMessage = New qqMail.MailMessage
objMail = objpop.PraseMsg(MsgId)
Dim strSubject As String = objMail.Subject.ToString
Dim strTextBody As String = objMail.TextBody.ToString
'Check for success/failure/error messages
If strSubject.IndexOf("Success") > 0 Or strSubject.IndexOf("Failure") > 0 Or strSubject.IndexOf("Error") > 0 Or _
strTextBody.IndexOf("SENT FAX REPORT") > 0 Or strTextBody.IndexOf("ERROR FAX REPORT") > 0 Then
'Check if the original recipient name can be found in the message
If strTextBody.IndexOf(recipientName) > 0 Or strTextBody.IndexOf(recipientName) > 0 Then
'*********************************************************************************************************
'Send response back to original sender
Dim objMail2 As qqMail.qqSmtp = New qqMail.qqSmtp
With objMail2
.To = origSender
.From = origRecipient
.TextBody = strTextBody.ToString
.Subject = strSubject.ToString
.AddAttachment(My.Settings.attpath & "\" & aAttachName)
.SmtpServer = My.Settings.mailserver
End With
objMail2.SendMail()
If objMail2.isOk = False Then
' there is an error.
Throw New Exception(objMail2.ErrDes)
End If
objMail2 = Nothing
'Delete attachment
If File.Exists(My.Settings.attpath & "\" & aAttachName) Then
File.Delete(My.Settings.attpath & "\" & aAttachName)
End If
'Delete message
objpop.Del(counter)
moveOn = True
Exit For
End If
End If
Next
End If
objpop.logout()
'if response was not received, put thread to sleep for 15 seconds
If moveOn = False Then
Threading.Thread.Sleep(15000)
End If
Loop Until moveOn = True
Catch ex As Exception
errhandler(ex)
Finally
'end thread
If Not t1 Is Nothing Then
t1.Abort()
End If
End Try
End Sub