Solved

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

Posted on 2004-09-12
8
180 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 85

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
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12040572
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now