[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.NET Prevent form from freezing while doing actions.

Posted on 2011-05-02
8
Medium Priority
?
764 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:altimofejevs
8 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35504171
run your DoServerRequest2 as a backgroundworker
(since you do not show any code of it, can not make code suggestions....)
0
 

Author Comment

by:altimofejevs
ID: 35504196
   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
 
LVL 11

Expert Comment

by:saragani
ID: 35504220
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:saragani
ID: 35504232
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
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35504240
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 35504310
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 35504340
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
 
LVL 17

Accepted Solution

by:
nepaluz earned 2000 total points
ID: 35504372
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question