. NET Framework lite multi-thread prompt
Abstract: Learn how to use multiple threads in Windows Forms applications through. NET Framework Lite version.
Introduction
Rich user experience is the main aspect of all interactive applications. Software that requires user interaction must respond to user activities as quickly as possible. At the same time, the application must be able to process data to display the results to users.
Multiple Threads can be used in an application to separate tasks executed on the user interface (UI) from those executed on the background. By organizing tasks in this way, the UI can respond to user input and process data by background processes.
Create auxiliary thread
An application can create one or more threads for execution. The first step for creating a secondary (or subordinate) thread is to create a threadstart proxy and specify the method to be executed by this thread. Then pass the threadstart proxy to the constructor of the thread class. For example, to start a new thread and execute the myfunction method, call the start method of the thread class as follows:
Update user interface from auxiliary thread
You can use control. invoke to update the user interface (UI) from other threads than the UI thread ). This method executes the proxy in the context of the control thread on the UI thread .. Net Framework Lite version only supports the heavy load control. Invoke method in the full version of. NET Framework. Control. invoke only uses one parameter: A proxy that specifies the method to be executed on the UI thread. The proxy must be of the eventhandler type and have the following signature:
Void myfunctionname (Object sender, eventargs E)
Note that to update the UI in the auxiliary thread, you must call application. doevents () in the code (). Call application. doevents () to ensure that any event triggered by the auxiliary thread is handled by the UI thread.
The following sample code describes how to create a secondary thread and update the ListBox control named listbox1 from the UI thread and the secondary thread:
To use this code, perform the following operations:
Use the Windows application template to create a new smart device application.
Add the ListBox control to the Windows form (default name: listbox1 ).
Add the statement using system. Threading; to the top of the form1.cs file.
Paste the code into the form1 class.
Call the dothreading method from the form1 constructor.