Principles and discussion of Windows service writing (4)

Source: Internet
Author: User
Tags thread

(iv) Discussion of some issues

In the previous chapters, there are some common principles of writing services, but there are some problems, writing simple services can not be seen, but encountered complex applications will have some problems, so this chapter is used to analyze, solve these problems, suitable for advanced applications of developers. The contents of my chapter are experimentally obtained and have practical significance.

As I said in the first chapter, it is the main thread of a service that executes the Ctrlhandler function, which receives various control commands, but actually handles the command and executes the ServiceMain thread. Now, when a service_control_stop arrives, how do you stop the service as a developer? In some of the source code I've seen, most of them simply call the TerminateThread function to force the service process to kill. But there should be a little bit of threading programming common sense should know that the TerminateThread function is the worst of the available calls, and the service thread will not get any chance to do the cleanup, such as clearing the memory, releasing the core object, and DLLs the notification that no thread has been destroyed.

So the appropriate way to stop a service is to activate the service thread in some way, stop it from continuing to provide the service, and then return it after the current operation and cleanup work has been done. This means that you must perform the appropriate thread communication between the Ctrlhandler thread and the ServiceMain thread. The best known internal thread communication mechanism is the I/O completion port (I/O completion ports), and if you write a large service that requires simultaneous processing of numerous requests and runs on a multiprocessor system, this model provides the best system performance. However, because of its high complexity, it is not worthwhile to spend a lot of time and effort on small-scale application, then as a developer, you can choose other communication methods, such as asynchronous process call queue, Socket and window message to adapt to the actual situation.

Another important issue when developing a service is the problem of all status reporting when calling the SetServiceStatus function. A lot of service developers are often arguing about when to call SetServiceStatus, and the general recommendation is to call the SetServiceStatus function first, report the Service_stop_pending State, The control code is then passed to the service thread or a new thread is created to continue the operation, and then the service is set to service_stopped, and the service stops just before the thread is about to perform the operation.

Related Article

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.