大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
不可以,但是能够在主线程的基础上调用委托(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函数执行结束)后执行。
最简单的方法是
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False'加入这句代码就可以在别的线程访问窗体控件了
End Sub
Timer 是在 主线程里执行的,Thread 是在自己的线程中执行的,多核心的CPU使用线程能提高处理能力。回到你的问题,主线程如果卡死,timer就会失去响应,很正常的。请使用System.Timers.Timer类吧。请参考一下:
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
建立委托。 Me.Invoke——同步委托 或者 Me.BeginInvoke——异步委托 例:Me.Invoke(Sub Textbox.Text="这是一个示例" End Sub)
多线程一般是不推荐用的,因为线程之间如果有共享资源的话会引起竞争,需要加锁处理;而且线程间没有时序关系,所以你在调试中可能会出现异步处理结束顺序与开始处理顺序不一致的情况(我在调试中已经发现该问题)。
针对你提出的这个问题,采用了多线程处理,利用的是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