Question : Program Not Responding

I'm running an application in VB.NET 2008 that scrape and input data to web
When the application has run for some time the windows form freezes and displays a (Not Responding) message in the title of the form.

how to prevent this?

Answer : Program Not Responding

Simple solution using a BackgroundWorker:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
Public Class Form1

    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Dim arrComputers() As String = TextBox5.Text.Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        Dim listToPing As New List(Of String)
        For Each strComputer As String In arrComputers
            listToPing.Add(strComputer.Trim)
        Next
        bgw.RunWorkerAsync(listToPing)
    End Sub

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim listToPing As List(Of String) = CType(e.Argument, List(Of String))
        Dim pingURL As String = ""
        Dim blogURL As String = "http://www.jeremywadsworth.com"
        Dim blogName As String = "Jeremy Wadsworth"
        For j As Integer = 0 To listToPing.Count - 1
            Try
                pingURL = listToPing.Item(j).ToString
                Dim technoratiPing As HttpWebRequest = CType(WebRequest.Create(pingURL), HttpWebRequest)
                technoratiPing.Method = "POST"
                technoratiPing.ContentType = "text/xml"
                Dim streamPingRequest As Stream = CType(technoratiPing.GetRequestStream, Stream)
                Dim xmlPing As XmlTextWriter = New XmlTextWriter(streamPingRequest, System.Text.Encoding.UTF8)
                xmlPing.WriteStartDocument()
                xmlPing.WriteStartElement("methodCall")
                xmlPing.WriteElementString("methodName", "weblogUpdates.ping")
                xmlPing.WriteStartElement("params")
                xmlPing.WriteStartElement("param")
                xmlPing.WriteElementString("value", blogName)
                xmlPing.WriteEndElement()
                xmlPing.WriteStartElement("param")
                xmlPing.WriteElementString("value", blogURL)
                xmlPing.WriteEndElement()
                xmlPing.WriteEndElement()
                xmlPing.WriteEndElement()
                xmlPing.Close()
                Dim technoratiPingResponse As HttpWebResponse = CType(technoratiPing.GetResponse, HttpWebResponse)
                Dim streamPingResponse As StreamReader = New StreamReader(technoratiPingResponse.GetResponseStream)
                Dim strResult As String = streamPingResponse.ReadToEnd
                streamPingResponse.Close()
                technoratiPingResponse.Close()
            Catch ex As Exception
                'Add code here to flag a service as broken
            End Try
        Next
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        MessageBox.Show("Done", "Ping Completed", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Button1.Enabled = True
    End Sub

End Class
Random Solutions  
 
programming4us programming4us