Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternative to do/until loop

Status
Not open for further replies.

jayy66

Programmer
Aug 21, 2006
56
US
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:
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
 
As long as you're using the timer from the Threading namespace, you should be fine. Don't use the other timers found in the framework, as they require a messagepump to be running in the background, and you won't have that in this case.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
thanks for the suggestion. it took me a while to figure
out the system.threading.timer deal but I got it working
now.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top