VB.NET ListView Delay in Displaying Items Added

Terry Rogers
Terry Rogers used Ask the Experts™
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
            End If

        'STEP FOUR - Setup MailBee Email Components

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


        If vUseSMTPServer.ToString <> "True" Then
            If vSMTPUsername.Length = 0 Then
                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
                If mailer.Send() Then
                    Dim lv As ListViewItem = lvStatus.Items.Add(Now.ToString("hh:mm:ss tt"))
                    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

        Next i

    End Sub

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014


Are you using above line? Add the


after that.
Most Valuable Expert 2012
Top Expert 2014
Add the


statement within the loop.
Most Valuable Expert 2012
Top Expert 2014

Why that grade then?
Terry RogersIT Technical Architect


Apologies. that grade is un-intended. Once the question is re-opened, ill grade correctly!
Terry RogersIT Technical Architect


Sorry for the mis-hap with the grade & thanks again! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial