大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
很简单,调用API函数CreateThread
我们提供的服务有:成都做网站、网站设计、微信公众号开发、网站优化、网站认证、雁山ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的雁山网站制作公司
但是用vb做多线程的关键不在于创建,而是在于线程的不稳定,其不稳定是由于vb6自身的控件(如按钮、文本框等)不是线程安全的,怎么用你程序就可能怎么挂,所以这些个玩样儿在多线程中统统不能用。
那么UI的实现也得用API实现。这样vb的多线程才相对要稳定。但是vb的可视化开发的优势又荡然无存。
过程只需要1个,线程你要多少就需要创建多少,这个可以用循环来创建:
Dim Thd(99) As Thread
For i As Integer =0 to 99
Thd(i)=New Thread(AddressOf Start)
Thd(i).Start()
Next
如果你不需要保留这些线程的引用
那个数组也可以省略掉
直接在循环中(New Thread(AddressOf Start)).Start()就行了
---------------------------------------------------------------------------
代码不是在IDE中打的,可能有细微小错误
多线程操控同一个空间容易出现混乱,可通过SyncLock语句,将对空间的操作添加至lock语句内部,
Class simpleMessageList
Public messagesList() As String = New String(50) {}
Public messagesLast As Integer = -1
Private messagesLock As New Object
Public Sub addAnotherMessage(ByVal newMessage As String)
SyncLock messagesLock
messagesLast += 1
If messagesLast messagesList.Length Then
messagesList(messagesLast) = newMessage
End If
End SyncLock
End Sub
End Class
参考:
Private Sub setA()
msgbox("")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t1 As Threading.Thread = New Threading.Thread(AddressOf setA)
t1.start
End Sub
多线程一般是不推荐用的,因为线程之间如果有共享资源的话会引起竞争,需要加锁处理;而且线程间没有时序关系,所以你在调试中可能会出现异步处理结束顺序与开始处理顺序不一致的情况(我在调试中已经发现该问题)。
针对你提出的这个问题,采用了多线程处理,利用的是BackgroundWorker也就是异步处理控件进行了处理。
代码已经经过调试通过。欢迎交流,如有问题,留下QQ或其他联系方式。
代码如下,并附程序截图。
‘---------------------------------------------------
Imports System.ComponentModel '导入异步控件命名空间
Public Class Form1
Private howmany As Integer = 10
Private AnalysisNumber(0 To howmany - 1) As BackgroundWorker
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
creatNewBackgroundWorker()
addHandle()
startWork()
End Sub
Private Sub creatNewBackgroundWorker()
For i As Integer = 0 To AnalysisNumber.Length - 1
AnalysisNumber(i) = New BackgroundWorker
Next
End Sub
Private Sub addHandle()
For i As Integer = 0 To AnalysisNumber.Length - 1
AddHandler AnalysisNumber(i).DoWork, AddressOf AnalysisNumber_DoWork
AddHandler AnalysisNumber(i).RunWorkerCompleted, AddressOf AnalysisNumber_RunWorkerCompleted
Next
End Sub
Private Sub startWork()
For i As Integer = 0 To 9
Dim temp(0 To 9) As Integer
For j As Integer = 1 To 10
temp(j - 1) = 10 * i + j
Next
AnalysisNumber(i).RunWorkerAsync(temp)
Next
End Sub
Private Sub AnalysisNumber_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim data As Integer()
data = CType(e.Argument, Integer())
Dim temp As Integer
For i As Integer = 0 To data.Length - 1
temp = data(i)
data(i) = temp * temp
Next
e.Result = data
End Sub
Private Sub AnalysisNumber_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Dim data As Integer()
data = CType(e.Result, Integer())
For i As Integer = 0 To data.Length - 1
ListBox1.Items.Add(data(i))
Next
End Sub
End Class