我有一個簡單的Windows服務應用程序,我試圖在VS 2008 IDE中進行調試,但每次運行代碼時,出現錯誤「嘗試讀取或寫入受保護的內存,這通常表示其他內存已損壞「。。在下面的service.Stop()
線會出現此錯誤:Windows服務錯誤:「嘗試讀取或寫入受保護的內存,這通常表示其他內存已損壞。」
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);
}
}
}
下面是一個簡單的Windows服務類
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
安裝時運行正常,以澄清某些事情,ServiceBase的OnStop方法沒有參數,因此不需要傳遞新對象[] {args} – pdiddy 2009-11-27 16:12:30