十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Sub Main() Dim thr As New Thread(AddressOf 循环) thr.Start("a") End Sub Sub 循环(a() As String) '这里随你干什么循环也行 For Each i As String In a MsgBox(i) Next End Sub
在网站建设、成都网站设计中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联公司专业成都网站建设十载了,客户满意度97.8%,欢迎成都创新互联客户联系。
不可以,但是能够在主线程的基础上调用委托(Invoke)。(主线程会被占用)
例子:
Dim thr As Threading.Thread
Public Delegate Sub VoidDelegate()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
thr = New Threading.Thread(AddressOf Where)
thr.Start()
End Sub
Public Sub Where()
Me.BeginInvoke(New VoidDelegate(AddressOf WhereThr))
End Sub
Public Sub WhereThr()
Me.TextBox.text="0123456789"
End Sub
(Invoke)这种情况下,主线程被占用,所有由主线程执行程序,都将被(wherethr函数执行结束)后执行。
Public Class Form1
Public Class SquareClass '把多线程调用的函数封装到类中,通过类事件返回
Public Value As Double
Public Square As Double
Public Event ThreadComplete(ByVal Square As Double)
Public Sub CalcSquare()
Square = Value * Value
RaiseEvent ThreadComplete(Square)
End Sub
End Class
Dim WithEvents oSquare As SquareClass
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '多线程返回值测试,当线程运行完成激发事件
oSquare = New SquareClass()
Dim t As New Threading.Thread(AddressOf oSquare.CalcSquare)
oSquare.Value = 30
t.Start()
End Sub
Sub SquareEventHandler(ByVal Square As Double) Handles oSquare.ThreadComplete '响应事件函数
MsgBox("The square is " Square)
End Sub
End Class
多线程并不一定就可以缩短时间,具体要看你上面的“过程”是否调用数据库完成。如果调用了数据库,可以考虑用存储过程替代“过程”。另外看看“过程”里是否反复打开和关闭了数据库连接,如果是,这才是真正的耗时动作。如果没调用数据库,可以考虑使用Parallel类(.net4以后)里面的函数,使多个任务在多核CPU上并行运行,缩短时间。
Sub Main()
Dim thr As Thread
For Pi As Integer=0 To 4 //启用5线程
MulParams =Pi vbTab sFile vbTab dFile vbTab 1 vbTab DelN vbTab cr vbTab cg vbTab cb vbTab IndexI
GlobalParamas(pi)=MulParams .Split(vbTab)
thr=New Thread(AddressOf MyMulThreadCaller)
thr.Start() //启动多线程进程
Application.DoEvents
Next
End Sub