Run code line by line?

Adooz

New member
Joined
Oct 24, 2009
Messages
4
Programming Experience
Beginner
Hello,

I am a beginner and i have a simple question. How can i run code in VB.NET line by line?

Here is an example :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = "Start"

Dim URL As String
Dim Info As String
Dim srcWebpage As New System.Net.WebClient
URL = "http://www.google.com"
Info = srcWebpage.DownloadString(URL)
srcWebpage.Dispose()
RichTextBox1.Text = Info

End Sub

How can i execute the first line(TextBox1.Text = "Start") first, and only after it is executed completely continue with other lines....

When i click the button the text "Start" will be displayed in TextBox1 only after the whole event has finished, after the webpage is loaded in RichTextBox1. What i need is the text in TextBox1 displayed before it starts downloading the webpage in RichTextBox1.

Thanks,
 
Each line does execute completely, the problem is that while your other lines in Click event is processed you are tying up the UI thread and prevent the application from repainting and handling other events. A quickfix if you expect the rest of the code to finish promptly is to call DoEvents method to allow message queue to process, thus force an immediate repaint. It should be sufficient in this case unless the request stalls.
VB.NET:
Application.DoEvents()
The best solution though is to not make calls in UI thread that "could take forever" to finish, a functionality called multi-threading exists that enables this. That means your application can run multiple threads at the same time, the UI thread run by itself handling basic UI interaction and one or more worker threads do the heavy lifting of processing data. I recommend you try out the BackgroundWorker component (in Toolbox) for you first experiences with multithreading.
 
Thanks alot for your help! It is working now :) I will also try out the BackgroundWorker component as you have suggested!
 
I have experimented with the BackgroundWorker component, used it on my code...and i got the basics about how to use it but i dont understand something....

Public Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim URL As String
Dim Info As String
Dim srcWebpage As New System.Net.WebClient
URL = "http://www.google.com"
Info = srcWebpage.DownloadString(URL)
srcWebpage.Dispose()
RichTextBox1.Text = Info

End Sub

Well, everything is working fine with this code but i would like to know if its possible to load the webpage to RichTextBox1 on another form using BackgroundWorker? When i change (RichTextBox1.Text = Info) to (Form2.RichTextBox1.Text = Info) the code still runs but it doesnt load the webpage to Form2.RichTextBox1 - the text in Form2.RichTextBox1 is empty ...its only working on the main form....
 
You should not access UI controls directly from other threads, in most cases that result in a CrossThreadException. You can pass the result of the BW job through e.Result property and handle it in the RunWorkerCompleted event, for other types of progress use the ReportProgress methods and handle the ProgressChanged event. These events are raised in the thread that created the component, ie usually the UI thread, and are therefore safe to use in UI thread. As you move to other types of multithreading you'll discover delegates and the Invoke call, but that's hardly ever needed when using the BW component.
 
Back
Top