Programming a windows service
I 开发者_运维技巧have started prgramming a windows service. I have added a notify icon from the toolbox. It has the small notify icon that appears in the systray as a member of those icons.
It works so far.
So far I have a blank form. I have used the DoubleClick for the notifyIcon to bring up the form (I will use the form for something later).
Now I have a list of things I want to accomplish to make this work like a true windows service.
First of all, if possible, I owuld like to remove the maximize and cancel button on the form. Most windos service apps that I have seen offer the ability to close the app by right-mouse-button clicking on the notify icon which brings up a menu of options.
I see in the properties of the form under Misc there is an CancelButton. But I do not see how do deactivate it. In the Properties of the forum I see under Window Style there is a ControlBox option that, if I turn to false, all three buttons, (minimize, maximize and cancel) go away.
These are not what i am looking for. I would not like the option for them to resize, maximize or close the form here. I suspect people will close the box intending to make the box go away while still wanting the app to run.
Under the "Focus" caption in Properties, there id "Deactivate". I have created my own event/method/function for this and in debug I noticed that when you click on the x-box in the upper right corner, this function is called. The problem is that after the function is over, the app closes anyway. How do I over-ride this function?
Secondly, how do you catch the right button click event on the notify icon in the systray?
I can see how to create events for "Click" and "MouseClick" etc. but how so I determine which button was click?
Using the right buton click is how such programs know when to pull up a menu. So I would like to know how to do this as well.
Windows services should not display notification icons, nor show forms. You'll find out very soon that post Vista they are explicitly forbid from doing so.
You need to split your application into a user program that may show an icon in the notification tray and shows forms, and the service. They need to be distinct processes and communicate via IPC (shared memory, named pipes, sockets etc).
As to how to react to a righ-click on the notification icon question, you simply need to assign something in your designer to the ContextMenu property:
The menu is shown when the user right-clicks an icon in the notification area of the taskbar. Shortcut menus also are known as pop-up menus.
Standard Windows services do not run with any user interface at all. Generally speaking, your Windows service project type in Visual Studio won't contain any UI components whatsoever.
The UIs you see when working with other services are secondary applications designed to manage those services. Those may run in the system notification area ("tray"), or may run as minimized windows, may be MMC snap-ins, or may be separate applications the administrator launches on an ad-hoc basis.
As an example, SQL Server runs as a service, whether or not Management Studio or any of the other ancillary UI apps are running.
In any event, the user interface "component" of a service needs to be an entirely separate application. This is particularly important in the post-Vista world, for reasons cited by Remus Rusanu. You'll create a separate WinForms project to do this, and have it send messages to the service to control. (This may include the basics such as start, stop and restart; it usually also includes other service-specific commands.)
You may want to consider reading the following other questions here on StackOverflow for some guidance on the standard Windows Service/UI helper application pattern:
- What can Services do under Windows?
- What is the difference between a windows service and a regular application?
- Creating a user interface for monitoring and interacting with a running windows service
- Running an exe from windows service that interacts the the user’s desktop
- Why do forms fail in Windows Services
精彩评论