0

I am learning to code since a few months. Today I want to code a password manager. Everything works fine but the get icon function has problem if a host is unreachable. This function is to get the favicon from the webpage.

           Try

            For Each myItem As ListViewItem In lv_data.Items

                Dim baseurl = myItem.Text
                Dim url As Uri = New Uri(baseurl)

                If url.HostNameType = UriHostNameType.Dns Then
                    Dim iconURL = "http://" & url.Host & "/favicon.ico"

                    Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
                    Dim response As System.Net.HttpWebResponse = request.GetResponse()
                    Dim stream As System.IO.Stream = response.GetResponseStream()
                    imglist.Images.Add(Image.FromStream(stream))
                    lv_data.Items.Item(myItem.Index).ImageIndex = myItem.Index
                End If

            Next
        Catch ex As WebException

        End Try

Some servers respond with a Exception :

Remote host could not be resolved

. When that happens the whole for each loop stops. I am looking for a way to ignore that error and proceed with the next item from the listview.

Can anybody give me a tip or maybe a solution.

Best regards, Der King

Der King
  • 21
  • 4
  • 1
    If you move your try/catch inside the foreach....next when you get the exception you can continue with the next iteration. – Steve Sep 30 '17 at 21:42
  • Thanks now it proceeds with the loop. But now the icons are not in the right place. I think this happens when a server is unreachable he is getting the icon from the next working server. – Der King Sep 30 '17 at 21:55

1 Answers1

0

First move the try catch inside the loop

For Each myItem As ListViewItem In lv_data.Items
    Try

       Dim baseurl = myItem.Text
       Dim url As Uri = New Uri(baseurl)
       ...
    Catch
    End Try
Next

This will allow your code to continue inside the foreach loop when it encounters an exception in retrieving the favicon.ico

The next problem is how you address the images in the imagelist. When you encounter the exception because your code cannot retrieve the favicon you don't add anything to the ImageList.
At this point your ImageList and your ListView.Items don't contain the same number of elements, thus you cannot use the myItem.Index to reference the image in the ImageList.

But, because the image has been just added to the ImageList in the previous line, then you are certain that the image required is the last element of the ImageList. So you can use the ImageList.Images.Count property (less 1)

imglist.Images.Add(Image.FromStream(stream))
myItem.ImageIndex = imgList.Images.Count - 1

(Notice that you can use directly myItem in this context)

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you so much. It works. Bad to say the speed is really slow. A listview with about 120 entries takes up to 4 min to load the icons. But i tink thats the problem of the webrequest and exceptions. – Der King Sep 30 '17 at 23:47
  • Yes, exceptions take a lot of time to process all the info required (stack trace etc). – Steve Oct 01 '17 at 07:03