James Wilson
James Wilson

Reputation: 5150

Force my console application to Sleep/Pause/Hibernate

I am running a non-threaded application that is responsible for checking the database for a file, updating the location and moving the file to a new directory.

However every Monday-Thursday-Saturday our server containing SQL Server reboots at 8:40 pm. At 8:30 pm and until 9:00 pm or from 8-9 if that is easier I need my program to sleep.

On those days at 8pm (or 8:30 pm) I would like my program to sleep/wait/pause until 9:00 pm (I can safely assume the reboot will be finished by this time).

Is this easy/possible? Can it be done without threading? If I do have to use threading is it easy to implement for someone who has never used threads?

Upvotes: 0

Views: 615

Answers (1)

Michael
Michael

Reputation: 1028

James, Try / Catch and using are not mutually exclusive.

If the approach you decide to take is retry on SqlException you should not, but could use:

Thread.Sleep(1800000);

This will wait 30 minutes. Then you could try your call again. I would advise against this, but it is a quick, dirty option.

This might be useful.

You should design your application to handle the situation where database connectivity is lost. You could batch your data that you intend to write and send it all once connectivity is reestablished.

Consider rewriting this as a service. Perhaps you want to log connectivity issues.

try
{
    using (SqlConnection sqlConnection = new SqlConnection(ConnectionProvider.ConnectionString(databaseName)))
    {
        using (SqlCommand command = new SqlCommand())
        {

            if (sqlConnection.State != System.Data.ConnectionState.Open)
            {
                sqlConnection.Open();
            }
            //command.configure etc...
            command.ExecuteNonQuery();

        }
    }
}
catch (SqlException ex)
{
    //retry
}

Upvotes: 1

Related Questions