Smartclient Learning (4): using multithreading to create highly responsive Smart Client Applications

Source: Internet
Author: User

Smartclient (Smart Client) note 4, mainly learning vs2005 multi-threadProgramDevelopment support, Lecture 7 in the webcast series: use multithreading to create high-response Smart Client Applications. program developers should know that every program running on the system is a process. each process contains one or more threads. the process may also be the dynamic execution of the entire program or some programs. A thread is a set of commands or special segments of a program. It can be executed independently in a program. it can also be understoodCodeThe running context. Therefore, the thread is basically a Lightweight Process, which is responsible for executing multiple tasks in a single program. Generally, the operating system is responsible for scheduling and execution of multiple threads.

Multithreading 1. Basic concepts (simple)

Use a processor only when needed

Do not use polling to synchronize threads

Use synchronization object

Multithreading increases the complexity of applications

2. Create and terminate a thread

The managed object is built on the Basic Windows Thread model. The system. Threading. Thread class encapsulates the windows thread function:
A. Creation thread:
B. Start the thread
It is very easy to start a thread, but it is expensive"
Thread mythread = new thread (newthreadstart (myworkerthread ));
Mythread. Start ();

C. Set thread priority

D. Suspend/resume thread execution

E. Terminate the thread:
The application can be terminated only when all threads end. (even if the UI is closed, the program is still running as long as there is a foreground thread continuing ). before the application is terminated, all foreground threads must be terminated.
Different methods can be used to terminate a thread.
Use the Boolean (Instance) Variable
Use background thread
Use thread. Abort (Force exception execution)
Use thread. Join to know when the thread is terminated (waiting for the thread to end)

Update the UI control in two threads 1. Control. Invoke

Controls a should only be updated by the threads that create them.

B. Use Control. invoke to delegate update controls from other threads

Both C synchronous and asynchronous invoke methods can be used

D. Support parameter transfer

E. When the UI control is updated from other threads, an exception is thrown.

2. Do not directly update the UI control (remember that vc6.0 supports direct updates)

Private void workerthread ()
{
Statusbar1.text = "workerthreadactive ";
While (! Workerthreaddone)
{
Thread. Sleep (1000 );
}
}

3. Correct Method: Use control. (BEGIN) invoke instead

Private delegate void labelupdater (string labeltext );
Private void workerthread ()
{
Labelupdaterlblupdater = new labelupdater (updatelabel );
This. Invoke (lblupdater, new object [] {"workerthreadactive "});
While (! Workerthreaddone)
{
Thread. Sleep (1000 );
}
}
Private void updatelabel (stringlabeltext)
{
Label1.text = labeltext;
}

Three threads and thread pool)

It is very expensive to use the usual method to create a thread. The thread pool is used for threads with relatively short lifecycles.
The thread pool is applied by the Common Language Runtime (CLR) and supports threading. Timer objects and asynchronous operations.
Threadpool Performance

1. Because threads in the thread pool can be reused, the overhead for creating threads when there are Idle threads in the thread pool is very small.

2. If no idle thread exists in the thread pool, create a new thread (when the thread pool has less than 25 threads) at the same cost as createthread.

3. If the thread in the thread pool is idle for a period of time (usually 60 seconds), the thread will be removed from the pool and its occupied resources will be released.

Four-thread security and synchronization object thread security

1. multi-threaded access to objects may cause problems

2. Read inconsistent data

3. The two threads may update data at the same time, resulting in incorrect results.

4. Use synchronization objects to create thread security classes

5. thread security is required only for classes requiring concurrent access

Synchronization object

Threads are generally not autonomous and must be coordinated by developers. We can use the following synchronization objects:

1. Interlocked

2. autoresetevent

3. manualresetevent

4. Monitor

5. mutex

Summary

Multithreading is important in program development. In addition to having used vc6.0 for a program job in college, subsequent work projects are not involved. however, multithreading is particularly important in the development of smart clients. The previous three notes are not at the core of the Smart Clients. Starting from this note, I gradually learned some of the main implementation methods of the Smart Clients.

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.