Solved

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

Posted on 2004-09-12
8
179 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now