. NET Framework lite multi-thread prompt

Source: Internet
Author: User
. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.