Jeg har lavet et lille program, som viser og rotere mellem forskellige web sider. Der bliver hentet url'er og tiden for hvor længe siden skal vises, ud fra en fil.
Når programmet har kørt i ca. en dags tid opstår der en memory leak og programmet fejler. Ud for
- Me.WebBrowser1.Url = New Uri(current.Url)
med fejlen "Ressourcen er i brug"
Er her nogen der kan fortælle mig hvor fejlen ligger og om der evt. var forbedringsforslag til koden?
- Imports System
- Imports System.Xml
- Imports Microsoft.Win32
-
- Public Class frmWebRotator
-
- Private urlTimeIndex As Integer
- Private _urlTimes As List(Of UrlTime)
-
- Public Property UrlTimes() As List(Of UrlTime)
- Get
- Return _urlTimes
- End Get
- Set(ByVal value As List(Of UrlTime))
- _urlTimes = value
- End Set
- End Property
-
-
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- With Me
- .MaximizeBox = False
- .MinimizeBox = False
- '.TopMost = True
- .FormBorderStyle = FormBorderStyle.None
- .WindowState = FormWindowState.Maximized
- End With
-
- 'Cursor.Hide()
- DisableScreensaver()
-
- Try
- Dim xmld As XmlDocument
- Dim xnodelist As XmlNodeList
-
- xmld = New XmlDocument()
- xmld.Load("c:\WebRotator.xml")
- xnodelist = xmld.SelectNodes("/root/webpage")
-
- Me.UrlTimes = New List(Of UrlTime)
- For Each node As XmlNode In xnodelist
- Dim urlTime As New UrlTime(node.ChildNodes.Item(0).InnerText, Integer.Parse(node.ChildNodes.Item(1).InnerText))
- Me.UrlTimes.Add(urlTime)
- Next
-
- 'Følgende linjer er til test.
- 'Me.UrlTimes.Add(New UrlTime("http://www.google.com", 5000))
- 'Me.UrlTimes.Add(New UrlTime("http://www.bing.com", 2000))
- 'Me.UrlTimes.Add(New UrlTime("http://eksperten.dk", 8000))
-
- Me.urlTimeIndex = Integer.MaxValue
- Me.Timer1.Interval = 1
- Me.Timer1.Start()
- Catch ex As Exception
- MessageBox.Show(e.ToString)
- End Try
-
-
- End Sub
-
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- Me.Timer1.Stop()
- Me.readyStateLabel.Text = "Loading..."
-
- If Me.urlTimeIndex >= Me.UrlTimes.Count Then
- Me.urlTimeIndex = 0
- End If
-
- Dim current = Me.UrlTimes(Me.urlTimeIndex)
-
- Me.Timer1.Interval = current.Duration
- Me.WebBrowser1.Url = New Uri(current.Url)
-
- Me.urlTimeIndex = Me.urlTimeIndex + 1
- End Sub
-
- Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
- Select Case e.KeyCode
- Case Keys.Escape
- Me.Close()
- End Select
- End Sub
-
- Private Sub WebBrowser1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles WebBrowser1.PreviewKeyDown
- Select Case e.KeyCode
- Case Keys.Escape
- Me.Close()
- End Select
- End Sub
-
- Private Sub DisableScreensaver()
- Dim regKey As RegistryKey
- Dim regVal As String
- regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
- regVal = regKey.GetValue("ScreenSaveActive")
-
- If regVal = 1 Then
- regKey.SetValue("ScreenSaveActive", "0")
- End If
-
- regKey.Close()
- End Sub
-
- Private Sub frmWebRotator_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
- Dim regKey As RegistryKey
- Dim regVal As String
- regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
- regVal = regKey.GetValue("ScreenSaveActive")
-
- If regVal = 0 Then
- regKey.SetValue("ScreenSaveActive", "1")
- End If
- regKey.Close()
- End Sub
-
- Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
- Me.Timer1.Start()
- Me.readyStateLabel.Text = "Ready - Luk vinduet på 'Esc'"
- End Sub
- End Class
-
- Public Class UrlTime
-
- Public Sub New(ByVal url As String, ByVal duration As Integer)
- Me.Duration = duration
- Me.Url = url
- End Sub
-
- Private _url As String
- Public Property Url() As String
- Get
- Return _url
- End Get
- Set(ByVal value As String)
- _url = value
- End Set
- End Property
-
-
- Private _duration As Integer
- Public Property Duration() As Integer
- Get
- Return _duration
- End Get
- Set(ByVal value As Integer)
- _duration = value
- End Set
- End Property
-
-
- End Class