How to debug Windows Services

Source: Internet
Author: User

When you write a Windows ServiceProgramAnd then install the service on your computer. if the service program has some logic errors, it will not be executed as expected. You want to debug it, but you do not know how to start it, because it is different from other common program debugging. Two methods are described here.

 

Method 1:

In fact, this method is not called debugging, but also a stupid method. It is to use MessageBox to output intermediate information of some programs, so that you can find out where the program has a problem, you can also use it in try catch and use MessageBox to pop up exceptions. This method is also intuitive. Unfortunately, there are also problems with implementation. Add a reference to using system. Windows. forms; and add a lineCodeMessageBox. Show (ex. tostring (), "error"); I thought this was the case, but after the service was re-compiled and started, it did not pop up. Think twice about it. Fortunately, the system logs are enabled in the service program, and related exceptions are recorded in the system logs, you can find it in the "Event Viewer" of "Computer Management. I found one,

The displayed information is

 "The displayed dialog box or form is invalid when the application is not running in userinteractive mode. Please specify servicenotification or defadeskdesktoponly style to display notifications sent by the service application ."

Baidu directly found an answer. When calling MessageBox, you also need to set its messageboxoptions attribute to defaultdesktoponly. Modify as follows:

MessageBox. Show (ex. tostring (), "error", messageboxbuttons. OK, messageboxicon. Information, messageboxdefaultbutton. button1, messageboxoptions. defaultdesktoponly );

Then re-compile, install and start again. OK. The error message is displayed successfully.

 

Method 2:

The second method is true debugging. In fact, Windows Services can still be debugged. It should be said that any type of program should be able to be debugged, and others should consider it when developing the compiler. The following describes the specific method.

We know that any program has a corresponding process. If your Windows service is started, there will also be a corresponding process. You can see it through the task manager.

We can use the "attach to process" function provided by the vs compiler for debugging. First, open your windows service source program and set a breakpoint in it. For example, in the onstart method protected override void onstart (string [] ARGs, then start your service. After it is started, find "attach to process" in the "debug" column of"

Find your process in the process list,

If you do not have one, check the process that is displayed for all users in the lower left corner. Then you can find the process of your Windows Service and click OK.

After clicking "OK", you may have waited for a long time and have not seen it hit the breakpoint. Haha, don't worry. I have omitted a point above. We started the service first and then attached the process, because the onstart method will be executed as soon as the service is started, we haven't attached the process of this service to vs yet? Therefore, this breakpoint set in the onstart method will certainly not hit. So we need to find a way, that is, to add a timer, we start this timer in the onstart method, and then set the timer interval to 1 minute, set a breakpoint in the timer event. In this way, the service is re-generated. After installation and startup, the process is appended to vs. After one minute, you will see that it hits the breakpoint. However, one premise is that you must append the process to vs within one minute. Otherwise, it will still fail.

If there is still a problem, check whether your Windows service is generated in release mode. If yes, change it to debug mode.

For more information, see Article Windows Service (with auxiliary service development tools)

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.