- VB.NET code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
url = "http://topic.csdn.net/t/20041007/15/3431114"
Dim th As New System.Threading.Thread(AddressOf URL1)
th.Start()
MsgBox(shuju)
End Sub
Private Sub URL1()
Dim s As New System.Net.WebClient()
shuju = System.Text.Encoding.GetEncoding("gb2312").GetString(s.DownloadData(url))
End Sub
第一次接触多线程。发现我点击玩按钮后 msgbox 直接给我弹出来了一个空内容~
也就是说下面的线程并没有执行结束但是 Button1_Click 里面的代码已经执行完毕了~
如果等待线程执行完毕后再执行 MsgBox(shuju)???
------解决方案--------------------------------------------------------
- VB.NET code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click url = "http://topic.csdn.net/t/20041007/15/3431114" Dim th As New System.Threading.Thread(AddressOf URL1) th.Start() While th.IsAlive Application.DoEvents() System.Threading.Thread.Sleep(5) End While MsgBox(shuju) End Sub Private Sub URL1() Dim s As New System.Net.WebClient() shuju = System.Text.Encoding.GetEncoding("gb2312").GetString(s.DownloadData(url)) End Sub
------解决方案--------------------------------------------------------
你把MsgBox放到线程里提示。。。就不卡了。本来也应该是子线程提示主线程完事的。。。
当然直接用MsgBox在子线程里好不好另说。
------解决方案--------------------------------------------------------
Public Class Form1
Dim url As String
Dim shuju As String
Private Delegate Sub showMsgbox(ByVal msg As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
url = "http://topic.csdn.net/t/20041007/15/3431114"
Dim showmsgbox As showMsgbox = AddressOf URL1
showmsgbox(url)
End Sub
Private Sub URL1()
Dim s As New System.Net.WebClient()
shuju = System.Text.Encoding.GetEncoding("gb2312").GetString(s.DownloadData(url))
MsgBox(shuju)
End Sub
End Class