[WM] Creates a console program that does not exit

Source: Internet
Author: User
Tags message queue

There's always a need to listen to something, you need to create a program that runs in the background. Obviously, the service can meet your needs, but I have not written the service program, do not know how to operate the specific (should be exposed to several export functions to the operating system), but you can use other ways to achieve the desired effect.

Windows programs persist because of their message mechanism. The message loop extracts messages from the message queue, distributes them to Windows, and the system calls the window procedure. The message loop, which is similar to a dead loop, is the reason the program will not quit (for the time being).

1 while (GetMessage(&msg,NULL,0,0))
2 {
3     TranslateMessage(&msg);
4     DispatchMessage(&msg);
5 }

If you want to run a program in the background, this message loop is essential. Now it's all about getting a message, two choices:

Process directly before distributing the message, inserting a bar across the middle of the message loop.

Or use the original window process, let the operating system to execute.

In the middle of a bar, looked very simple and easy to understand, but it is said that the window process is performed by the operating system, because the operating system needs to allocate time slices to the program, if you do it, the operating system does not know how many time slices to allocate. Or use the original window process, create a window class, creating a window. All we need to do is , don't let that window show .... (In addition, I can not think how to let the window handle and the window process, which people know to be able to guide twos)

When CreateWindow is created, you can specify that the window display is not displayed. (previously tested on WIN32, did not pay attention to the WM experiment ...) Results create a window that is directly displayed)

01 hWnd = CreateWindow(szWindowClass,
02                     szTitle,
03                     WS_DISABLED,//丫的就是这个参数
04                     CW_USEDEFAULT,
05                     CW_USEDEFAULT,
06                     CW_USEDEFAULT,
07                     CW_USEDEFAULT,
08                     NULL,
09                     NULL,
10                     hInstance,
11                     NULL); 

Later vs default generated code, it will be Showwindow,updatewindow, only the two lines can be commented out

1 //ShowWindow(hWnd, nCmdShow);
2 //UpdateWindow(hWnd);

The rest is findwindow,sendmessage, or postmessage.

Ps:

This is also true under. NET cf. Application.Run (Form f) is actually a message loop +f.show (). You can p/invoke a message loop yourself, and it's your own business to show it.

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.