create and Control Windows Services--Not a Slap Sh
2024-07-21 02:21:28
供稿:网友
not a slap shot, but almost as fast
creating the service using .net is fairly straightforward. you use the .net framework's system.serviceprocess namespace and four classes within it: servicebase, serviceinstaller, serviceprocessinstaller, and servicecontroller. these classes provide most of the functionality you need to create a service (see figure 1).
figure 1 | everything you need in one namespace. click here.
the servicebase class defines methods a derived class can override so the service control manager can control a service—including onstart(), onstop(), onpause(), and oncontinue(). furthermore, a service can override the oncustomcommand() function to carry out specific operations sent to it programmatically by an external service controller (more on this later). implementing these functions in your derived class gives you the necessary structure of a service:
protected override void onstart(string[] args)
{
}
protected override void onstop()
{
}
protected override void onpause()
{
}
protected override void oncontinue()
{
}
allow your service to begin execution by defining an entry point into your executable, as you would with a regular executable. within the main() function, create an instance of your class and call the run() method defined by the servicebase base class. if you want your executable to support multiple services, create an array of servicebase objects with each element corresponding to one of your defined services:
static void main()
{
system.serviceprocess.servicebase[] myservices;
myservices = new system.serviceprocess.servicebase[] { new service1, new service2() };
system.serviceprocess.servicebase.run(myservices);
}
note that run() does not start your service. in other words, you won't receive an onstart() call from the run() method itself. as i mentioned before, the service control manager controls the service by calling the necessary controlling functions.
国内最大的酷站演示中心!