Avatar of Terry Rogers
Terry Rogers
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

VB.NET ListView Delay in Displaying Items Added

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

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Terry Rogers

8/22/2022 - Mon