这里介绍的不是专业术语,只是有些问题的解决方法,多线程的一个比较大的作用就是可以保持用户界面的响应(不假死),这就要让子线程去做相对复杂的任务,这里会收集多线程的实例,理论可能很少,只求问题的解决。Control.CheckForIllegalCrossThreadCalls = False
例一:在webbrowser中把navigate方法用子线程去完成
- Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
- Dim tNav As New Thread(AddressOf navUrl)
- tNav.Start()
- End Sub
- Private Sub navUrl() '导航过程
- myUrl = txtUrl.Text
- web1.Navigate(myUrl)
- End Sub
例二:使用控件BackgroundWorker
- Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e2 As System.ComponentModel.DoWorkEventArgs) Handles bkWorker2Repair.DoWork
- Dim worker2 As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
- e2.Result = RepairAccess(worker2, e2)
- End Sub
- Private Sub BackgroundWorker2_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bkWorker2Repair.ProgressChanged
- 'msgInfo2("正在压缩中...", "")
- End Sub
- Private Sub BackgroundWorker2_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkWorker2Repair.RunWorkerCompleted
- 'Me.Text = "good"
- 'msgInfo2("正在压缩中...", "")
- ' First, handle the case where an exception was thrown.
- If Not (e.Error Is Nothing) Then
- MessageBox.Show(e.Error.Message)
- ElseIf e.Cancelled Then
- ' Next, handle the case where the user canceled the
- ' operation.
- msgInfo2("已取消写入", "")
- Else
- ' Finally, handle the case where the operation succeeded.
- '’ msgInfo2("操作成功", "")
- 'Me.Text = "ok"
- End If
- msgInfo("共:" & dataNum & "条,目前:", dataVolume)
- Me.bkWorker2Repair.CancelAsync()
- Me.bkWorker2Repair.Dispose()
- End Sub
- Public Function RepairAccess(ByVal worker2 As System.ComponentModel.BackgroundWorker, ByVal e2 As System.ComponentModel.DoWorkEventArgs) As Long
- Try
- dbName = My.Settings.dbNamer
- Dim dbName2 As String = "\" & dbName & ".mdb" '设置数据库名
- Dim objJRO As Object = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"))
- dataNum = EXESQLScalar("select count(*) from INFO") '总数量
- objJRO.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & dbName2, "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\dataBackup.mdb")
- Kill(Application.StartupPath & dbName2)
- FileSystem.Rename(Application.StartupPath & "\dataBackup.mdb", Application.StartupPath & dbName2)
- dataVolume = MyRound(((GetFileSize(dbName)) / 1024 / 1024), 2).ToString & "M"
- Catch ex As Exception
- MsgBox("数据库名称或地址出错,请检查!", MsgBoxStyle.OkOnly)
- Exit Function
- End Try
- End Function
最后是调用
If bwSoso.IsBusy = True Then Exit Sub
BackgroundWorker2.RunWorkerAsync()
===========================
取消(中止)线程的方法
首选在某个相关停止线程的事件中写代码: BackgroundWorker.RunWorkerAsync() (注:作用是让BackgroundWorker.CancellationPending = True)
然后在BackgroundWorker Dowork事件中加入如下代码
If BackgroundWorker.CancellationPending = True Then
e.Cancel = True '中止线程 '应该写在Dowork 事件或它包含的函数或过程中
Exit Function '中止函数或事件,这里是退出函数,如果是过程那就是exit sub
End If