简体   繁体   中英

Windows service stop automatically

I want to call a stored procedure from windows service and after that I want to self stop the service. Here is what I have:

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

The problem I'm having is that, when I start the service, it stops automatically at instance and shows a message box.

I have change the serviceProcessInstaller's property Account to LocalSystem. Now it's working

Not sure I totally understand your appraoch, but this should simply be a case of calling base.OnStop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM