In " Multi-threaded and delegated implementation of waiting form (loading is loading interface), run timeout can cancel the operation > Multithread programming is used in this article, so take a note here.
We continue to use it.< Delegation: Synchronization, Asynchronization, Lambda Expressions and Built-in Delegation > The sample code of this article is the example of this article. In the original article, asynchronous delegation is used to invoke the synchronous method, that is:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Temporary removal Button1 Button Click Event RemoveHandler Button1.Click, AddressOf Button1_Click '****************************************** Dim method As MethodInvoker = Sub() 'Simulate long-term work Code For i As Integer = 0 To 100 'Simulate long-term work Threading.Thread.Sleep(100) 'Display progress Dim Actiondelegate As Action(Of Integer) = Sub(int) Me.Label1.Text = int.ToString & "%" End Sub Me.Label1.Invoke(Actiondelegate, i) Next End Sub method.BeginInvoke(Nothing, Nothing) '****************************************** 'Remember to recover after work Button1 Button Click Event AddHandler Button1.Click, AddressOf Button1_Click End Sub
Now it's changed to thread, the code is as follows:
Private th As Thread Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Temporary removal Button1 Button Click Event RemoveHandler Button1.Click, AddressOf Button1_Click '****************************************** th = New Thread(New ThreadStart(Sub() 'Simulate long-term working code For i As Integer = 0 To 100 'Simulate long-term work Threading.Thread.Sleep(100) 'Display progress Dim Actiondelegate As Action(Of Integer) = Sub(int) Me.Label1.Text = int.ToString & "%" End Sub Me.Label1.Invoke(Actiondelegate, i) Next End Sub)) th.Start() '****************************************** 'Remember to recover after work Button1 Button Click Event AddHandler Button1.Click, AddressOf Button1_Click End Sub
Running code, consistent with the effect of asynchronous delegation, attempts to close the form halfway through the run, reporting an error: System. ComponentModel. Invalid Asynchronous StateException: "An error occurred when calling a method. Target threads no longer exist. For example,
This is because the Me. Label 1. Invoke (Action delegate, i) code needs to be executed in the main thread (UI thread), and the main thread has been destroyed after closing the form, but the sub-thread th is still running, so the error is reported. The subthread th should be closed while the form is closed. Add the following code:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing If th.ThreadState <> ThreadState.Stopped Then th.Abort() End If End Sub
If you click the button several times, you will still get an error. It may be that you run multiple sub-threads, but only close one. The best way is to set the thread as the background thread. When the main thread closes, the background thread will also close together. Just add th.IsBackground = True before the th.Start() statement.
The above is using Lambda expressions, and you can also use AddressOf to associate delegates. As follows:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Temporary removal Button1 Button Click Event RemoveHandler Button1.Click, AddressOf Button1_Click '****************************************** th = New Thread(AddressOf aa) th.IsBackground = True 'Background thread th.Start() '****************************************** 'Remember to recover after work Button1 Button Click Event AddHandler Button1.Click, AddressOf Button1_Click End Sub Private Sub aa() SyncLock lockObj For i As Integer = 0 To 100 'Simulate long-term work Threading.Thread.Sleep(100) 'Display progress 'Me.Label1.Text = i.ToString & "%" 'Modified to Dim Actiondelegate As Action(Of Integer) = New Action(Of Integer)(AddressOf bb) Me.Label1.Invoke(Actiondelegate, i) Next End SyncLock End Sub Private Sub bb(ByVal int As Integer) 'Display progress Me.Label1.Text = int.ToString & "%" End Sub
It can be seen that using multithreaded programming is not as difficult as imagined, but in fact similar to using delegation:
First, you need to introduce a namespace: Imports System.Threading
Second, define a thread, which is simple: Dim th As Thread
Then instantiate, using AddressOf or Lambda expressions. th = New Thread(AddressOf procedure name)
Then use the Start method to run the thread: th.Start()
ThreadState or IsAlive attributes are used to monitor thread status, and processing measures, such as closing threads with the Abort method: th.Abort()
The following commands are commonly used by threads:
Implication of command
Start causes the thread to start running
Sleep pauses the thread for a period of time. Format: Thread. Sleep (milliseconds)
Suspend thread pauses when it reaches the security point
Abort stops threads
Resume restarts suspended threads
Join causes the current thread to wait for another line result
The following table is a common thread property:
Attribute value
IsAlive If the thread is active, the value is True
IsBackground Gets or sets a Boolean value to indicate whether a thread is allowed to be a background thread
Name gets or sets the name of the thread
Priority gets or sets an operating system to prioritize thread calls
ApartmentState gets or sets the thread model used by a particular thread
ThreadState contains values describing the state of threads
Refer to Thread Synchronization and Forms under Threads< Delegation: Synchronization, Asynchronization, Lambda Expressions and Built-in Delegation>.
For more blog articles on multithreaded programming, please refer to:
1,Using VB.NET to Develop Multithread
2,VB.NET Multithreading
3,Thread's rough life
4,Analysis of Multithread Programming Technology in VB.NET2010
5. Official Help< Managed Thread Processing>