banner



How To Debug Windows Application

  • Updated date Sep 11, 2012
  • 212k
  • 4

This article helps in running the Windows Service in debug mode then that nosotros tin debug the service to check the catamenia of code or fixing any of the issues.

This article helps in running the Windows Service in debug way so that nosotros can debug the service to bank check the flow of code or fixing whatsoever of the issues. In this article, we are not going to concentrate more on developing a Windows Service rather we concentrate on developing a uncomplicated service which opens a Notepad process.

Note: In real-time applications we will not exist using a Windows Service to open some application like Notepad but more than for creating the lawmaking that runs backside the scenes and performs background operations which takes a long duration to complete. For instance, we want to cheque the condition of some flag in a table and practise an appropriate action and update the flag to something like completed and this should be running 24*7 unless we are in server maintenance.

Open Visual Studio 2010/2008 and create a Windows Service application as below:

Selecting-Windows-Service-Project-Type.jpg

Figure ane: Selecting a Windows Service Project Type

Once the projection is created, you will run across the post-obit files created for the Windows Service. In this case we are not going to stress out ourselves with creating setup projects or writing likewise much business logic.

Note: In existent-time applications we need to add project installer files and create a setup project to deploy the windows service in the server.

Solution-structure-windows-service.jpg

Figure two: Solution construction

First things kickoff. The principal reason for this commodity is to identify why we need to do something to make the service that runs an exe. Why cannot a Windows Service be run in your desktop? Why cannot we exist debug information technology directly?

If you are interested in knowing the answers to the preceding questions, practise non wait until the end of the commodity. But click on the F5 button and run across what happens.

alert-in-windows-service.jpg

Figure iii: Alert

Y'all will see the alert by Visual Studio that you lot cannot start a service from the command line or a debugger. It says that nosotros need to install this service kickoff to run the service. This is proficient for when nosotros want to deploy it, correct? Only, what if we want to debug our code to run across whether it is running properly or not? Or if we want to debug our lawmaking to fix some issue. Of grade if nosotros follow Test Driven Development, nosotros should take washed unit testing for the code we take developed. Merely what if we need to check the lawmaking in the Windows Service classes? Anyways, the answer is simple: You lot cannot practise that direct.

OK. Now become to the Service1.cs course and bank check the lawmaking. Information technology should be something as in the post-obit:

Service-class-before-customization.jpg

Figure 4: Service class earlier customization

Here the OnStart() method is what to do to initiate a service and OnStop() takes care of what to practice stop the service. If we want to make our service code to be executed for every time interval we may have to add a timer control and add together the logic that runs whenever the timer time interval is elapsed.

Nosotros cannot use the OnStart and OnStop methods to run the service on your desktop and then nosotros need to write our own methods simulating the OnStart and OnStop methods in the preceding figure.

Then hither are the steps nosotros demand to follow to reach the goal of running the Windows Service like an exe file on your desktop and also should exist able to debug the code:

i. Declare a static variable of type System.Timers.Timer.

//Declare a timer static variable
private static  System.Timers. Timer  _aTimer;

2. Add the timer related lawmaking in the constructor of the service.

//Now enable the timer and add an outcome handler for elapsed
public  Service1()
        {
          _aTimer =
new  Arrangement.Timers. Timer (30000);
          _aTimer.Enabled =
true ;
           _aTimer.Elapsed +=
new  System.Timers. ElapsedEventHandler (_aTimer_Elapsed );
          InitializeComponent();
        }

3. Write the custom method to commencement the timer and end the timer:

//Custom method to Start the timer
public void  Start()
 {
     _aTimer.Showtime();
 }
//Custom method to Stop the timer
public void
 Terminate()
 {
     _aTimer.Cease();
 }


4. Now call the Beginning() and Stop() methods in the Service class's OnStart and OnStop methods:
 //Call the custom commencement method
protected override void  OnStart( string [] args)
 {
this .Start ();
 }
//Telephone call the custom stop method
protected override void  OnStop()
 {
this .Stop ();
 }

5. Handle the Timer Elapsed event.
//Handle the Timer Elapsed event
void  _aTimer_Elapsed( object  sender, System.Timers. ElapsedEventArgs  e)
{
//Create an instance of Procedure
Process  notePad = new Process ();
//Set up the FileName to "notepad.exe"
    notePad.StartInfo.FileName = "notepad.exe" ;
//Start the process
    notePad.Start();
//You may accept to write extra code for handling exit code and
//other System.Process treatment code
}


This completes the changes we take to make in the service grade. At present nosotros need to make changes in the Program.cs file.

The main role of achieving the goal is to apply preprocessor directives. You can learn more about preprocessor directives from the commodity posted by Sukesh http://www.c-sharpcorner.com/UploadFile/SukeshMarla/preprocessor-directive-in-C-Precipitous/

Practice the following changes in the Plan.CS file:

  1. Define Preprocessor directive #If DEBUG #else

0 Response to "How To Debug Windows Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel