vb.net线程的方法 vb 线程

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里如何设置多线程?首先 , 你把你那些要运行很久的过程 。尽量放在一个过程中 。
因为线程只能是过程,不能使函数,没有返回值的 。
然后,在某个事件下这样写:
**这里我假设你的那个很就的过程叫做 sub aaa()
调用如下:
Dim mythread As New System.Threading.Thread(AddressOf aaa)
mythread.Start()
***********
这样就是定义一个线程,名字叫 mythread , 这个线程会运行aaa这个过程 。
start 就是让线程运行 。
在vb.net中,多线程如何使用Sub Main()Dim thr As New Thread(AddressOf 循环)thr.Start("a")End SubSub 循环(a() As String)'这里随vb.net线程的方法你干什么循环也行For Each i As String In aMsgBox(i)NextEnd Sub
VB.NET 简单多线程多线程一般是不推荐用的,因为线程之间如果有共享资源的话会引起竞争,需要加锁处理;而且线程间没有时序关系,所以你在调试中可能会出现异步处理结束顺序与开始处理顺序不一致的情况(我在调试中已经发现该问题) 。
针对你提出的这个问题,采用了多线程处理,利用的是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 * ij
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 = https://www.04ip.com/post/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 = https://www.04ip.com/post/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 如何设计多线程运行Sub Main()
Dim thr As Thread
For Pi As Integer=0 To 4 //启用5线程
MulParams =PivbTabsFilevbTabdFilevbTab1vbTabDelNvbTabcrvbTabcgvbTabcbvbTabIndexI
GlobalParamas(pi)=MulParams .Split(vbTab)
thr=New Thread(AddressOf MyMulThreadCaller)
thr.Start() //启动多线程进程
Application.DoEvents
Next
End Sub
vb.net中如何结束一个线程vb.net中如何结束一个线程
一般而言,如果您想终止一个线程,您可以使用System.Threading.Thread类的Abort方法. 例如:
Dim worker As ThreadStart = New ThreadStart(AddressOf workerthreadmethod)
Dim t As Thread = New Thread(worker)
t.Start()
MessageBox.Show("Wait for a while for the thread to start.")
MessageBox.Show(t.ThreadState.ToString())
t.Abort()
MessageBox.Show(t.ThreadState.ToString())
t.Join()
MessageBox.Show(t.ThreadState.ToString())
当然,在调用Abort方法后,线程并不是立刻终止,要等线程的所有finally快中的代码完成后才会完全终止. 所以在主线程中可以用Join方法来同步,当线程还未完全终止时,t.Join()将处于等待,直到t线程完全结束后再继续执行后面的语句 。
Abort方法是会导致线程跳出一个异常错误的 , 你需要在代码中捕获该异常 。下面是一个比较完整的VB.NET线程例子:
【vb.net线程的方法 vb 线程】Imports System
Imports System.Threading
Public Class MyTestApp
Public Shared Sub Main()
Dim t As New Thread(New ThreadStart(AddressOf MyThreadMethod))
'Start the thread
t.Start()
MsgBox("Are you ready to kill the thread?")
'Kill the child thread and this will cause the thread raise an exception
t.Abort()
' Wait for the thread to exit
t.Join()
MsgBox("The secondary thread has terminated.")
End Sub
Shared Sub MyThreadMethod()
Dim i As Integer
Try
Do While True
Thread.CurrentThread.Sleep(1000)
Console.WriteLine("This is the secondary thread running.")
Loop
Catch e As ThreadAbortException
MsgBox("This thread is going to be terminated by the Abort method in the Main function")
End Try
End Sub
End Class
Thread.Abort()方法用来永久销毁一个线程,而且将抛出ThreadAbortException异常 。使终结的线程可以捕获到异常但是很难控制恢复,仅有的办法是调用Thread.ResetAbort()来取消刚才的调用,而且只有当这个异常是由于被调用线程引起的异常 。因此,A线程可以正确的使用Thread.Abort()方法作用于B线程,但是B线程却不能调用Thread.ResetAbort()来取消Thread.Abort()操作 。
关于vb.net线程的方法和vb 线程的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读