Du skall enkelt kunna skapa en ny tråd från en tråd, tänk bara på att visa trådar väntar till deras bakgrundstrådar kör färdigt, andra inte.
Så om du har en tråd och startar 5 nya trådar i den. Då kan du antingen låta "huvudtråden" vänta tills alla 5 är färdiga innan den dör, eller så dör dina "undertrådar" när din huvudtråd dör. Läs mer om BackgroundThread i hjälpen.
Du skall ALDRIG ALDRIG accessa en kontroll från en annan tråd än den som skapade kontrollen. Detta kan ge dig obehagliga problem som du kommer felsöka dig till döds efter, eftersom det fungerar bra ibland, men ibland inte.
Läs på om Control.InvokeRequired i hjälpen, här kommer litet utdrag:
Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control. There are four methods on a control that are safe to call from any thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of these invoke methods when calling from a different thread.
For more information about multithreaded Windows Forms controls, see Multithreaded Windows Forms Control Sample.
Ja äntligen, efter mycket slit så fungerar det ( hyffsat)
Imports System.Threading
Public Class NewForm
Inherits System.Windows.Forms.Form
Private t As New Thread(New ThreadStart(AddressOf start))
Private t2 As New Thread(New ThreadStart(AddressOf Check))
Delegate Sub myNewForm()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t.Start()
t2.start()
End Sub
Sub start()
While True
Label1.Text += 1
t.Sleep(20)
End While
End Sub
Sub Check()
While True
Select Case Label1.Text
Case 100, 200, 300
Me.Invoke(New myNewForm(AddressOf startNew))
End Select
End While
End Sub
Sub startNew()
Dim frm As New newForm2()
frm.Show()
End Sub
End Class
267 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2