Debugging a Service

Debugging a service is not just a matter of pressing F5. You must attach the debugger to the running service, and you can only attach it after the service is started. Especially if you want to debug the code at the beginning of the CheckURLs() method, you'll find it helpful to slow the code down a little by inserting this line at the very top of the function:

 
 Threading::Thread::Sleep(2*60*1000); 

This causes the service to pause for two minutes, which should be long enough for you to attach to the process. Set a breakpoint on the line immediately after this one, stop the service, and build it. Then move quickly to attach to the process, as follows :

  1. Start the service.

  2. Choose Debug, Processes.

  3. Make sure the Show System Processes check box is selected.

  4. Scroll the Available Processes box to the bottom and select URLChecker.exe.

  5. Click Attach.

  6. On the Attach to Process dialog box, ensure Common Language Runtime and Native are selected, and then click OK.

  7. Click Close.

  8. Wait for execution to pause at your breakpoint.

If more time elapses than the pause you added, try setting a breakpoint on this line:

 
 while (!stopping) 

Control should reach this line once per second, giving you a chance to step through the loop. If you need more time to attach the debugger, stop debugging, stop the service, change the line of code that sleeps the service so that it sleeps longer, and repeat the attachment steps.

Once the debugger is attached to the service, you can do everything you normally would do: step through code, examine variables , and most importantly, see error messages and exceptions. When your service is not attached to a debugger, messages directed to the user (such as exception dialog boxes) are not shown; there is no one to show them to. When you are debugging, you see these messages. This should dramatically ease the difficulty of getting a more complex service working.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net