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()
    {
    }
}