We help IT Professionals succeed at work.
Get Started

VB.NET ListView Delay in Displaying Items Added

784 Views
Last Modified: 2012-05-11
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

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE