User Interface Threading Implementation

Source: Internet
Author: User
Tags message queue

Tips for user Interface threading implementation

1. Problem description

There are two types of threads in MFC, called Worker threads and user interface threads, respectively. The main difference between the two is that the worker thread has no message loop, and the user interface thread has its own message queue and message loop.

Worker threads do not have a messaging mechanism, and are typically used to perform background calculations and maintenance tasks, such as lengthy calculation procedures, printer spooling, and so on. User interface threads are typically used to handle user input that is independent of other threads ' execution, respond to events and messages generated by users and systems, and so on. However, for WIN32 API programming, there is no difference between these two threads, both of which can start a thread to perform a task with only the thread's start address.

2. Implementation Tips

When a Windows application runs, it automatically generates a main thread, which is processed by the main thread, and other threads can be created and used in the main thread. User interface threads are typically used to process user input and respond to various events and messages.

The function AfxBeginThread () that enables a user-interface thread is a different overloaded form of the same function as a worker-enabled function, and the function is declared as follows:

cwinthread* AfxBeginThread (
Cruntimeclass* Pthreadclass,
int npriority = Thread_priority_normal,
UINT nstacksize = 0,
DWORD dwcreateflags = 0,

);

Pparam: A 32-bit argument passed to the thread function, and the execution function interprets the value in some way. It can be a numeric value, or a pointer to a struct, and can even be ignored.
?
Npriority: The priority of the thread. If 0, the thread has the same priority as its parent thread.
?
Nstacksize: The thread allocates its own stack size, in bytes. If Nstacksize is set to 0, the thread's stack is set to the same size as the parent thread stack.
?
Dwcreateflags: If 0, the thread starts executing immediately after it is created, and if Create_ SUSPEND, the thread is suspended immediately after it is created.
?
Lpsecurityattrs: A thread's security property pointer, typically null.

3. Instance Code

BOOL cmythread::initinstance ()
{
Getmainwnd ()->setwindowtext ("Set title bar text from thread");
return TRUE;
}
Ctestview message handlers
#include "MyThread.h"
void Ctestview::onrbuttondown (UINT nflags, CPoint Point)
{
Cmythread *pthread;
Pthread= (cmythread*) AfxBeginThread (Runtime_class (Cmythread));
Cview::onrbuttondown (nflags, point);
}

4. Summary

In the Visual C + + 6.0 programming environment, we can either write a C-style 32-bit WIN32 application or use the MFC class library to write C + + style applications, each with its pros and cons. WIN32-based application execution is small, efficient, but requires the programmer to write a lot of code, and need to manage the system to provide all the resources of the program, and the MFC class library-based applications can quickly build applications, the class library for programmers to provide a large number of encapsulation classes, and developer Studio provides programmers with tools to manage user source programs, with the drawback that class library code is huge. Because of the advantages of being quick, simple, and powerful with class libraries, Visual C + + recommends the use of MFC class libraries for program development unless there is a special need.

http://blog.csdn.net/lizheng308/article/details/6195438

User Interface Threading Implementation

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.