VB.NET Prevent form from freezing while doing actions.

Hi, my application is communicating with webserver and when the internet speed is not high enough form may freeze form some time. I would like to know how to prevent that.

Example:
 
Try
            PreLoad()
            PreSave()
            LimitSettingsPreCheck()
            DoServerRequest2("connect.php?id=" & PCID)
        Catch
        End Try

Open in new window

altimofejevsAsked:
Who is Participating?
 
nepaluzCommented:
you need to declare a public variable for your page string inside the class and initialize (and populate it) on calling the BGW, i.e

Dim MyCall As New NewBGW
MyCall.page = "yourpagestring"
RepWorker.RunWorkerAsync(MyCall)

Open in new window


declare the page string between line and 2 and three in the last class code suggestion, i.e

Public Class NewBGW
    Public page As String
.......

Open in new window

0
 
nepaluzCommented:
run your DoServerRequest2 as a backgroundworker
(since you do not show any code of it, can not make code suggestions....)
0
 
altimofejevsAuthor Commented:
   Public Function DoServerRequest2(ByVal page As String) As String
        Try
            Dim w As System.Net.WebRequest = System.Net.WebRequest.Create(E3MCEncrypter.Decrypt("6C8CDA0E295BF7C83FCC40FD96C1E32BAAA387DE23F8986C407C13CEE1AB5842") & page)
            Dim sw2 As Stream = w.GetResponse().GetResponseStream()
            Dim sr As New StreamReader(sw2)
            Dim response As New String(sr.ReadToEnd)
            sw2.Close()
            sr.Close()
            Return response
        Catch ex As Exception
            Return ""
        End Try
    End Function
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
saraganiCommented:
Depending on your Framework...

If 3.5 and lower, I would suggest using BackgroundWorker or ThreadPool

If 4.0 and higher (well there isn't any higher version of .Net framework right now, but there will be), then use Parallel or Tasks.

Please note that since you are starting DoServerRequest2 on a different thread, it will not block your code... meaning that it will continue with the lines the call for the Thread or Task start even if DoServerRequest2 was not completed.
You will also not get the exceptions on DoServerRequest2 since it is running on a different thread and it is out of the Try Catch scope.
0
 
saraganiCommented:
I started writing my reply before seeing your new post... The Try Catch I was talking about is the one on your first post.
The on on the DoServerRequest2 will still work properly.
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
You can use Asynchronous calls or can use Multi Threading
Please follow the links for further help:
http://www.visualbuilder.com/showCode.php?id=81369&scd_id=2836

http://www.codeproject.com/KB/threads/threadingvbnet.aspx

Thanks
0
 
nepaluzCommented:
heres a complete "working" background worker class for you
    Public WithEvents RepWorker As New System.ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
    Public Class NewBGW
        Public Class ResClass
            Public xResponse As String
        End Class

        Public Sub BeginRun(ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs)
            worker.WorkerReportsProgress = True
            Dim state As New ResClass
            Try
                Dim w As System.Net.WebRequest = System.Net.WebRequest.Create(E3MCEncrypter.Decrypt("6C8CDA0E295BF7C83FCC40FD96C1E32BAAA387DE23F8986C407C13CEE1AB5842") & page)
                Dim sw2 As IO.Stream = w.GetResponse().GetResponseStream()
                Dim sr As New IO.StreamReader(sw2)
                Dim response As New String(sr.ReadToEnd)
                sw2.Close()
                sr.Close()
                e.Result = response
            Catch ex As Exception
                e.Result = Nothing
            End Try

        End Sub
    End Class

    Private Sub RepWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles RepWorker.DoWork

        Dim worker As System.ComponentModel.BackgroundWorker
        worker = CType(sender, System.ComponentModel.BackgroundWorker)

        Dim WC As NewBGW = CType(e.Argument, NewBGW)
        WC.BeginRun(worker, e)
    End Sub

    Private Sub RepWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles RepWorker.RunWorkerCompleted
        Dim xResult As String = e.Result
        If Not String.IsNullOrEmpty(xResult) Then
            'do your bit here the response is valid
        End If
    End Sub

Open in new window

0
 
nepaluzCommented:
I have removed some code from the above, and to call the class, just do a
Dim MyCall As New NewBGW
RepWorker.RunWorkerAsync(MyCall)

Open in new window

and here is the class again
    Public WithEvents RepWorker As New System.ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
    Public Class NewBGW
        Public Sub BeginRun(ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs)
            worker.WorkerReportsProgress = True
            Try
                Dim w As System.Net.WebRequest = System.Net.WebRequest.Create(E3MCEncrypter.Decrypt("6C8CDA0E295BF7C83FCC40FD96C1E32BAAA387DE23F8986C407C13CEE1AB5842") & page)
                Dim sw2 As IO.Stream = w.GetResponse().GetResponseStream()
                Dim sr As New IO.StreamReader(sw2)
                Dim response As New String(sr.ReadToEnd)
                sw2.Close()
                sr.Close()
                e.Result = response
            Catch ex As Exception
                e.Result = Nothing
            End Try

        End Sub
    End Class

    Private Sub RepWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles RepWorker.DoWork
        Dim worker As System.ComponentModel.BackgroundWorker
        worker = CType(sender, System.ComponentModel.BackgroundWorker)

        Dim WC As NewBGW = CType(e.Argument, NewBGW)
        WC.BeginRun(worker, e)

    End Sub

    Private Sub RepWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles RepWorker.RunWorkerCompleted

        Dim xResult As String = e.Result
        If Not String.IsNullOrEmpty(xResult) Then
            'do your bit here the response is valid
        End If
    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.