troubleshooting Question

VB.NET ListView Delay in Displaying Items Added

Avatar of Terry Rogers
Terry RogersFlag for United Kingdom of Great Britain and Northern Ireland asked on
Visual Basic.NET.NET Programming
5 Comments1 Solution786 ViewsLast Modified:
I am adding the status of emails sent to a listbox immidiately after sending, however they entry into the listbox doesn't appear until the entire the sub routine has completely finished executing.
Private Sub cmdProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcess.Click
        ' STEP ONE - Setup INI File
        Dim objIniFile As New IniFile(Application.StartupPath & "\settings.ini")

        ' STEP TWO - Get Settings from INI File
        Dim vCustomerEmailDirectory As String = objIniFile.GetString("Settings", "vCustomerEmailDirectory", "")
        Dim vOtherAttachmentsDirectory As String = objIniFile.GetString("Settings", "vOtherAttachmentsDirectory", "")

        Dim vFromEmailAddress As String = objIniFile.GetString("Settings", "vFromEmailAddress", "")
        Dim vFromName As String = objIniFile.GetString("Settings", "vFromName", "")
        Dim vRecipientEmailAddress As String = objIniFile.GetString("Settings", "vRecipientEmailAddress", "")
        Dim vBCCRecipient As String = objIniFile.GetString("Settings", "vBCCRecipient", "")

        Dim vUseSMTPServer As String = objIniFile.GetString("Settings", "vUseSMTPServer", "")
        Dim vSMTPServer As String = objIniFile.GetString("Settings", "vSMTPServer", "")
        Dim vSMTPPort As String = objIniFile.GetString("Settings", "vSMTPPort", "")
        Dim vSMTPUsername As String = objIniFile.GetString("Settings", "vSMTPUsername", "")
        Dim vSMTPPassword As String = objIniFile.GetString("Settings", "vSMTPPassword", "")

        Dim vMessageSubject As String = objIniFile.GetString("Settings", "vMessageSubject", "")
        Dim vMessagePriority As String = objIniFile.GetString("Settings", "vMessagePriority", "")
        Dim vMessageBody As String = objIniFile.GetString("Settings", "vMessageBody", "")
        Dim vAltMessageBody As String = objIniFile.GetString("Settings", "vAltMessageBody", "")

        ' STEP THREE - Create Customer Collection & Populate with Directory Names from vCustomerEmailDirectory
        Dim vCustomers As New Collection
        Dim fFileSystemInfo As FileSystemInfo
        Dim dDir As New DirectoryInfo(vCustomerEmailDirectory)
        For Each fFileSystemInfo In dDir.GetFileSystemInfos()
            If fFileSystemInfo.Attributes = FileAttributes.Directory Then
                vCustomers.Add(fFileSystemInfo.Name.ToString)
            End If
        Next

        'STEP FOUR - Setup MailBee Email Components

        Smtp.LicenseKey = "MN600-1DD5EA7BD5B7D56FD51316C6DA1E-1CEA".ToString
        mailer = New Smtp

        mailer.DnsServers.Clear()
        mailer.SmtpServers.Clear()

        If vUseSMTPServer.ToString <> "True" Then
            mailer.DnsServers.Autodetect()
        Else
            If vSMTPUsername.Length = 0 Then
                mailer.SmtpServers.Add(vSMTPServer.ToString)
            Else
                mailer.SmtpServers.Add(vSMTPServer.ToString, vSMTPUsername.ToString, vSMTPPassword.ToString, AuthenticationMethods.SaslLogin Or AuthenticationMethods.SaslPlain)
            End If
        End If

        'STEP FIVE - Cycle from Customer Object and Send Mails to Each Customer

        For Each i In vCustomers

            ' STEP 1 - Setup Customer INI File Access
            Dim CustIniFile As New IniFile(vCustomerEmailDirectory.ToString & i.ToString & "\settings.ini")

            ' STEP 2 - Set from Email, BCC Email & Subject Fields
            mailer.From.Email = vFromEmailAddress.ToString
            mailer.From.DisplayName = vFromName.ToString
            mailer.Bcc.AsString = vBCCRecipient.ToString          

            ' STEP 3 - Get Customer Name & Email Address

            Dim vCustomerName As String = CustIniFile.GetString("Settings", "RecipientName", "")
            vCustomerName = StrConv(vCustomerName, VbStrConv.ProperCase)
            Dim vCustomerEmailAddress As String = CustIniFile.GetString("Settings", "RecipientAddress", "").ToLower

            ' STEP 4 - Set To Displayname and Email Address 
            mailer.To.AsString = vCustomerName.ToString & "<" & vCustomerEmailAddress.ToString & ">"

            ' STEP 5 - Setup Message Subject & Body

            mailer.Subject = vMessageSubject.Replace("%custname%", vCustomerName).Replace("%custid%", i.ToString).ToString
            mailer.Message.BodyHtmlText = vMessageBody.Replace("%custname%", vCustomerName).Replace("%custid%", i.ToString).ToString
            mailer.Message.BodyPlainText = vAltMessageBody.Replace("%custname%", vCustomerName).Replace("%custid%", i.ToString).ToString

            ' STEP 6 - Send Email
            Try
                If mailer.Send() Then
                    Dim lv As ListViewItem = lvStatus.Items.Add(Now.ToString("hh:mm:ss tt"))
                    lv.SubItems.Add(i.ToString)
                    lv.SubItems.Add(vCustomerEmailAddress)
                    lv.SubItems.Add("Sent Successfully")
                End If
            Catch ex As MailBeeException
                MessageBox.Show(ex.Message, "Error")
                If TypeOf ex Is MailBeeConnectionException Then
                    MessageBox.Show("Typical reason is blocked SMTP port. The port may be blocked by firewall, antispam or antivirus software installed on your system or your ISP's network.")
                End If
            End Try

            ' STEP 7 - Reset Message for Next Customer

            mailer.Message.Attachments.Clear()
            mailer.ResetMessage()
        Next i





    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros