在主线程完成之前,新创建的线程不会触发

时间:2013-07-24 16:47:31

标签: vb.net multithreading winforms

我是线程的新手,我只是创建一个单独的线程来运行一个大函数。我已经创建了一个消息框,在程序结束时显示在函数的末尾,告诉我它所花费的加载时间。当我加载应用程序时,消息框将显示它花费的时间,然后线程将启动(虽然UI是可导航的,而组件从线程加载)不是线程的点,以便能够处理多个功能同时?在新线程开始之前,为什么要等到主线程结束?

我在应用程序的早期声明并启动新线程

2 个答案:

答案 0 :(得分:2)

对于应用程序命名空间中的每个Form,将在Forms属性下的My命名空间中创建一个默认实例。

---------------------- /启动主线程 ------------- ----------------------

Private Sub FindCustomerLocation()
Dim Findcontractor_Thread As New Thread(AddressOf **FindContractor_ThreadExecute**)
Findcontractor_Thread.Priority = ThreadPriority.AboveNormal
Findcontractor_Thread.Start(**me**)
End Sub

------------------ /正在运行主题/ ---------------

Private Sub **FindContractor_ThreadExecute**(beginform as *NameOfFormComingFrom*)
Dim threadControls(1) As Object
threadControls(0) = Me.XamDataGrid1
threadControls(1) = Me.WebBrowserMap

 **FindContractor_WorkingThread**(threadControls,beginform) ' ANY UI Calls back to the Main     UI Thread MUST be delegated and Invoked

End Sub

------------------ /如何从线程设置UI调用/ ------------ ---------

Delegate Sub **FindContractor_WorkingThread**(s As Integer,beginform as           *NameOfFormComingFrom*)
Sub **FindContractor_WorkingThreadInvoke**(ByVal s As Integer,beginform as     *NameOfFormComingFrom*)
If beginform.mouse.InvokeRequired Then
Dim d As New FindContractor_WorkingThread(AddressOf            FindContractor_WorkingThreadInvoke)
beginform.Invoke(d, New Object() {s,beginform})
Else
 beginform.Mouse.OverrideCursor = Cursors.Wait

    'Do something...

beginform.Mouse.OverrideCursor = Nothing
 End If
End Sub

Sources from Pakks answer and Tested!

答案 1 :(得分:1)

如果您希望它们按照您的想法(同时)运行,则必须创建多个线程。请查看此链接并尝试创建多个线程。这应该有助于你的问题。干杯

http://msdn.microsoft.com/en-us/library/ck8bc5c6%28v=vs.80%29.aspx