Writing Windows services in Managed C + +

Source: Internet
Author: User
Tags thread visual studio

For years, when it comes to writing Windows services, you'll think of writing in Visual C + +, which is what a C + + programmer can do, something that VB programmers can't do. Previously, we referred to them as "services" or "NT Services", which are now named Windows Services and can be easily written with vb.net or C #.

But what if you want to write in managed C + +? After all, most experienced Visual C + + programmers write one or two of services and know how to do a similar project. Suppose you have a program that has to be run to provide services, and connect to some remote computers, if you don't want to write a manual, tell your customer to remember to rerun the program after each reboot, and you should make it a service, and suppose you have a convenience maintenance tool for deleting expired database records, If you don't want your administrator to run it once a week, you should make it a service. It looks very appealing, so let's get started.

Create a service project

The following things are simple: Open Visual Studio.NET, create a new project, and, under Visual C + + engineering, select the Windows service (. NET). Next, take a name that is conveniently found in the computer's list of services for this service, Cgnotifier. The wizard creates a class that inherits from System::serviceprocess::servicebase and opens the Design view, where you can put a timer, a database connection, or other components that are not visible.

Let's go to Code view to see the generated code, where there is a constructor and a Dispose method, both of which you can ignore, and a pair of overloaded methods: OnStart () and OnStop). In OnStart (), you can write the code required for the service. An important category of services is the use of "event-raising objects", such as an instance of System::io::filesystemwatcher, which can generally be created in OnStart (), in this case you can add event methods to a class, and handle the process during a service run The events raised by these objects. There is another kind of service, they do not react to what happens, perform specific tasks at specific times of day or week, usually in hibernation, but because their working status is constant, they should not be stopped or put into a loop. Check whether they have been stopped at a specific time.

The OnStart () method is the beginning of the service and is returned after execution, and the service generally does not appear as started until this method completes. This means that you cannot put a frequently used loop in OnStart (), or any method that is called directly from elsewhere. The most straightforward approach is to set up a separate method and call it in a new thread, as follows:

private:
bool stopping;
int loopsleep; //毫秒
Threading::Thread* servicethread;

protected:
//设置好服务应做的工作
void OnStart(String* args[])
{
  Threading::ThreadStart* threadStart =new Threading::ThreadStart(this,mainLoop);
  servicethread = new Threading::Thread(threadStart);
  servicethread->Start();
}
void mainLoop()
{
  loopsleep = 1000; //毫秒
  stopping = false;
  while (!stopping)
  {
   Threading::Thread::Sleep(loopsleep);
  }
}

This loop will run until the service stops because OnStop () has the STOP flag set:

void OnStop()
{
  stopping = true;
}

If you increase the Loopsleep value, the response time of the service will be increased when the stop is stopped.

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.