关于多线程和多核处理器运行时关系的几点疑问?
本帖最后由 pengwei0417 于 2014-01-22 23:09:06 编辑 我用VB.NET2008写程序,以下代码的运行时间做了一下对比:
一、按钮单击事件中直接运行
Dim dt1 As Date = Now
For i As Integer = 0 To 9999
TextBox10.Text = i
Next
MsgBox((Now - dt1).TotalMilliseconds)
Dim dt1 As Date = Now
For i As Integer = 0 To 9999
Application.DoEvents()
TextBox10.Text = i
Next
MsgBox((Now - dt1).TotalMilliseconds)
'测试多线程
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
'线程中过程
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim d1 As Date = Now
For i As Integer = 0 To 9999
Me.Invoke(New sw(AddressOf uuu), i)
Next
MsgBox((Now - d1).TotalMilliseconds)
BackgroundWorker1.CancelAsync()
End Sub
Private Delegate Sub sw(ByRef i As Integer)
Private Sub uuu(ByRef i As Integer)
TextBox10.Text = i.ToString
End Sub