Solved

My form blocks when i download an html page code. WHY????

Posted on 2004-09-12
8
186 Views
Last Modified: 2010-04-23
Hy,

This is my code. I download the html content of an Web Page and during this my form is not accessible. I can't press any tab's of my tabcontrol, i can't move my form from point x to point y. What should i do to make my form accessible while the code runs.

Dim req As WebRequest = WebRequest.Create(url)
        Dim res As WebResponse = req.GetResponse()
        Dim iTotalBuff As Integer = 0
        Dim Buffer(128) As [Byte]
        Dim stream As Stream = res.GetResponseStream()
        iTotalBuff = stream.Read(Buffer, 0, 128)
        Dim strRes As New StringBuilder("")
        While iTotalBuff <> 0
            Refresh()
            TextBox2.Refresh()
            Refresh()
            strRes.Append(Encoding.ASCII.GetString(Buffer, 0, iTotalBuff))
            Refresh()
            iTotalBuff = stream.Read(Buffer, 0, 128)
            Refresh()
            TextBox2.Refresh()
        End While

Thanks a lot.
0
Comment
Question by:drcyrus3d
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12040391
From the helpfile:

"The GetResponse method provides synchronous access to the WebResponse. For asynchronous access, use the BeginGetResponse and EndGetResponse methods."

Synchronous means the code will stop until the GetResponse call is complete.

There is an example in the help file on how to use these two methods.

Idle_Mind
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12040499
You can always start a new thread using threading and solve this kind of issues.
Put that code part you posted inside a Sub - End Sub. Then call that sub with a new thread.

Sub GetHTML()
        Dim req As WebRequest = WebRequest.Create(url)
        Dim res As WebResponse = req.GetResponse()
        Dim iTotalBuff As Integer = 0
        Dim Buffer(128) As [Byte]
        Dim stream As Stream = res.GetResponseStream()
        iTotalBuff = stream.Read(Buffer, 0, 128)
        Dim strRes As New StringBuilder("")
        While iTotalBuff <> 0
            Refresh()
            TextBox2.Refresh()
            Refresh()
            strRes.Append(Encoding.ASCII.GetString(Buffer, 0, iTotalBuff))
            Refresh()
            iTotalBuff = stream.Read(Buffer, 0, 128)
            Refresh()
            TextBox2.Refresh()
        End While
End Sub
Then create a thread and start it....
            Dim NewProcess As New System.Threading.Thread(AddressOf GetHTML)
            NewProcess.Start()

-Baan
0
 
LVL 1

Author Comment

by:drcyrus3d
ID: 12040521
Idle_mind i like your idea... but i don't have the MSDN. :(((((

Can you help me with the code to make it async???
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12040607
DotNetLover_Baan's solution is much easier.

=)
Idle_Mind
0
 
LVL 1

Author Comment

by:drcyrus3d
ID: 12049228
WHen i declare my sub i have a variable: Private Sub GetSource(ByVal url As String)

called url. I use this when i call it like : GetSource("http://www.google.com")

I did as DotNetLover says but in the statement:

Dim NewProcess As New System.Threading.Thread(AddressOf GetSource)

Underlines the GetSource beause it waits for my url variable. If i put anything in there as i ussualy would it's still underlined. What's the problem???

0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 12049595
Hi there,
using AddressOf operator in multithreading application, doesn't allow you to pass argument or get any parameter out of it.
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconParametersReturnValuesForFreeThreadedProcedures.asp

The easier way will be, to declare the "url" variable as global, and later using the value in it.
-Baan
0
 
LVL 1

Author Comment

by:drcyrus3d
ID: 12049969
Thanks a lot DotNetLover_Baan. It worked perfectly.

Thank you all for your quick help.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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