Windows服务自动停止

本文关键字:服务 Windows | 更新日期: 2023-09-27 18:29:40

我想从windows服务调用一个存储过程,然后我想自行停止该服务。这是我所拥有的:

public DailyChecker()
{
    InitializeComponent();
}
protected override void OnStart(string[] args)
{
    SqlConnection connection = new SqlConnection(connectionString);
    try
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand("sp_ChangeState", connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
    }
    finally
    {
        connection.Close();
    }
}
protected override void OnStop()
{
    base.OnStop();
}

我遇到的问题是,当我启动服务时,它会在实例中自动停止,并显示一个消息框。

Windows服务自动停止

我不确定我是否完全理解你的理解,但这应该只是一个调用base的例子。OnStop

try
{
    connection.Open();
    SqlCommand cmd = new SqlCommand("sp_ChangeState", connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.ExecuteNonQuery();
}
finally
{
    connection.Close();
}
base.OnStop();

我已将serviceProcessInstaller的属性Account更改为LocalSystem。现在它正在上运行