vb.net两个多线程,vb60 多线程
VB.NET 简单多线程
多线程一般是不推荐用的,因为线程之间如果有共享资源的话会引起竞争,需要加锁处理;而且线程间没有时序关系,所以你在调试中可能会出现异步处理结束顺序与开始处理顺序不一致的情况(我在调试中已经发现该问题)。
创新互联是专业的汾阳网站建设公司,汾阳接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行汾阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
针对你提出的这个问题,采用了多线程处理,利用的是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
VB.NET 多线程运行问题
多线程是用于处理复杂项目的
打个比方
你的主程序线程A中有个循环,由于代码是一行行走的,所以循环结束前下面的代码无法运行,而此时主界面的反应就类似卡死的样子,你点击按钮也没有反应,因为主线程在忙着循环呢,所以对按钮的事件代码要等待了,如果要避免这种情况,就要用到多线程,另开一个新线程专门用来执行循环代码,主界面就不会卡死了,只要在循环结束后将结果传回主线程调用就可以了,再复杂点要涉及到委托,控制了
按你的要求其实你的代码用不到多线程,只要把sleep放到两段代码中间就可以了。
新线程结束用thread.abort()
VB.net 如何设计多线程运行
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
vb.net的多线程问题为什么两个线程同时开始,导致每次输出结果不同
你真心无聊。
多线程就是这样的。界面线程是主线程,你这个Form_Load就是运行在主线程上的线程,而Thread1,Thread2是由主线程启动的。这个启动不是线性的。
主线程只是通知系统,请启动一个线程运行某某函数。
而哪个线程先运行完全在系统决定。甚至可能主线程执行到Thread1.Abort() ,Thread2.Abort()的时候这两个线程都还没有启动起来,所以你会遇到两个变量都是空值这种情况。
VB.NET 多线程简化步骤问题
过程只需要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中打的,可能有细微小错误
求助vb.net处理多线程的问题,想用委托来做以下代码。两个button控件和两个textbox控件。
Public Class Form1
Delegate Sub MySubDelegate(ByVal txt As String,ByVal num as integer)
Private Sub txtW(ByVal txt As String,ByVal num as integer)
Dim msgd As New MySubDelegate(AddressOf Me.txtW1)
Me.Invoke(msgd, txt,num)
End Sub
Private Sub txtW1(ByVal txt As String,ByVal num as integer)
if num=1 then
TextBox1.Text=txt
else
TextBox2.Text=txt
end if
End Sub
Dim t1, t2 As Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub autoAction1()
For i = 0 To 100
Delay(10)
txtw(i.ToString,1)
Next
t1.Abort()
End Sub
Private Sub autoAction2()
For i = 0 To 100
Delay(10)
txtw(i.ToString,2)
Next
t2.Abort()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Threading.Thread(AddressOf autoAction1)
t1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
t2 = New Threading.Thread(AddressOf autoAction2)
t2.Start()
End Sub
Public Sub Delay(ByRef Interval As Double) 'Interval单位为毫秒
Dim time As DateTime = DateTime.Now
Dim Span As Double = Interval * 10000
While ((DateTime.Now.Ticks - time.Ticks) Span)
Application.DoEvents()
End While
End Sub
End Class
当前文章:vb.net两个多线程,vb60 多线程
网站路径:http://scjbc.cn/article/hcoiss.html