vb.net 如何传递类参数线程结束后利用委托生成事件返回,线程应用包括传入和传出参数 。
Public Delegate Sub ThreadCallback(value As ThreadResult)
Public Class Form1
Private WithEvents _th_1 As Thread_1
Protected Overrides Sub OnLoad(e As System.EventArgs)
Dim value As ThreadObject
value.Index = 1
Me._th_1 = New Thread_1(Me)
Me._th_1.Run(value)
MyBase.OnLoad(e)
End Sub
Private Sub Thread_1_End(sender As Object, e As ThreadEventArgs) Handles _th_1.ThreadEnd
Me.TextBox1.Text = e.Result.Text
End Sub
End Class
Public Class Thread_1
Public Event ThreadEnd(sender As Object, e As ThreadEventArgs)
Private _control As Control
Sub New(control As Control)
Me._control = control
End Sub
Public Sub Run(value As Object)
Dim th As New Threading.Thread(AddressOf ThreadProc)
th.Start(value)
End Sub
Private Sub ThreadProc(obj As Object)
Dim value As ThreadObject = CType(obj, ThreadObject)
Dim result As ThreadResult = Nothing
If value.Index = 1 Then result.Text = "测试"
Dim callback As New ThreadCallback(AddressOf ThreadInvoke)
_control.Invoke(callback, result)
End Sub
Private Sub ThreadInvoke(value As ThreadResult)
RaiseEvent ThreadEnd(Me, New ThreadEventArgs(value))
End Sub
End Class
Public Structure ThreadObject
Public Index As Integer
'Public Rect As Rectangle
End Structure
Public Structure ThreadResult
Public Text As String
'Public Rect As Rectangle
End Structure
Public Class ThreadEventArgs
Inherits System.EventArgs
Private _result As ThreadResult
Public ReadOnly Property Result As ThreadResult
Get
Return _result
End Get
End Property
Sub New(value As ThreadResult)
Me._result = value
End Sub
End Class
vb.net 线程 如何动态 address of 过程,即 "过程" 是可以用户动态传送的 。过程名是不可能作为参数使用的,想必你需要调用的过程是有限的,那么可以用一个变量来确定:
sub 过程名(p as integer)
Dim i As Threading.Thread
select case p
case 1
i = New Threading.Thread(AddressOf 过程1)
case 2
i = New Threading.Thread(AddressOf 过程2)
.........................
..........................
end select
i.start()
end 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用子线程进行大批量数据传输,UI卡死,请高手指教 。代码如下试试这个怎么样,添加在子进程里面 , 就加在你批量传输代码里的每一个传输后面,也就是大批量中的每传输一个数据就暂停一下,而不是每一个大批量才暂停一下
System.Threading.Thread.Sleep(10) '让它走慢一点
【vb.net中线程传递 vbnet 多线程】关于vb.net中线程传递和vbnet 多线程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。
推荐阅读
- 安卓系统手机信号强弱,安卓系统手机信号强弱怎么调
- 开电视睡觉什么心理,开着电视睡觉心理原因
- js怎么设置input的颜色,js修改input背景色
- 电视上演员头发浓密怎么办,电视里的人头发白色哪里坏了
- linux用什么分隔命令 linux分隔符有哪些
- oracle分析函数范围,oracle分析函数用法
- 手机如何编辑ppt播放,手机上编辑ppt
- 用什么煮饭还好吃视频,用什么水煮饭更省力
- mysql事务死锁怎么办 mysql 事务sql