views:

752

answers:

4

I have a simple windows service application I am trying to debug in VS 2008 IDE but each time I run the code, I get the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." . This error occurs at the service.Stop() line below:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        ServiceBase[] servicesToRun;
        servicesToRun = new ServiceBase[] 
  { 
   new Service1() 
  };

        if (Environment.UserInteractive)
        {
            Type type = typeof(ServiceBase);
            BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
            MethodInfo method = type.GetMethod("OnStart", flags);

            foreach (ServiceBase service in servicesToRun)
            {
                method.Invoke(service, new object[] { args });
            }

            Console.WriteLine("Press any key to exit");
            Console.Read();

            foreach (ServiceBase service in servicesToRun)
            {
                service.Stop();//ERROR OCCURS HERE!
            }

        }
        else
        {
            ServiceBase.Run(servicesToRun);
        }            
    }
}

Below is the simple windows service class

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }
}
A: 

Have you tried remove the

Console.WriteLine("...");
Console.ReadLine();

I've no idea what impact this would have on a windows service but try replacing them with a

System.Threading.Thread.Sleep(5000);

To simulate a 5 second pause between the services starting & stopping.

Eoin Campbell
That too didn't work but when I invoke the "OnStop" method using reflection like I did with the "OnStart", I don't get the errors.
Tawani
A: 

Does this happen when you're not debugging?

Also, is this an exception? If so, then you should post the complete exception, including Inner exceptions and stack traces.

John Saunders
This happens only when I am debugging (Environment.UserInteractive), but runs fine when installed as a windows service
Tawani
A: 

Have you tried to add the following code line to your overridden OnStop() method?

this.RequestAdditionalTime(4000);

According to the Microsoft API documentation it is intended to be called by the overridden OnContinue, OnPause, OnStart, or OnStop methods to request additional time for a pending operation.

Halvard
+2  A: 

If you want to take full management of starting the ServiceBase outside ServiceBase.Run(), then you should also do the same trick with stopping it:

MethodInfo stopMethod = type.GetMethod("OnStop", flags); 

foreach (ServiceBase service in servicesToRun) 
{
    stopMethod.Invoke(service, new object[] { args }); 
}
taoufik
just to clarify something, the OnStop method of ServiceBase has no argument, so no need to pass new object[] {args}
pdiddy