Solved

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

Posted on 2004-09-12
8
181 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12040572
0
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET bulk update  of a table from another table 4 44
Advice in Xamarin 21 78
Web Form VB.Net  import CSV 4 36
VB.NET 2008 Populate DataModel with DataTable 44 28
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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