简体   繁体   中英

How can I stop my WinForms app for 10 minutes?

I have a C# Winforms program that sends Order data to a 3rd party API. The program runs on a server and has a timer to check for data to send to the API every 10 seconds.

My problem is that on occasion the 3rd party's server goes down. When that happens I don't want my program trying to send data and then creating an email to users telling them that their Sales Order failed every 10 seconds.

Here is my code that sets up the timer and calls the job every 10 seconds:

        private static void SetTimer()
    {
        aTimer = new System.Timers.Timer(10000);
        aTimer.Elapsed += OnTimedEvent;
        aTimer.AutoReset = true;
        aTimer.Enabled = true;
    }

The job itself isn't important, it runs w/oa problem.

If the API doesn't respond I get an error between 500 and 503 so I wrote this:

                if (statusCode > 500 || statusCode < 504)
            {

                aTimer.Enabled = false;
                System.Threading.Thread.Sleep(600000);
                aTimer.Enabled = true;
                return;
            }
            else
            {
                // This will post the migration status for anything EXCEPT a  server failure
                PostMigrationStatus(DocEntry, JobSuccess, jobID);
            }

The error handling isn't working, it seems that after a 500-503 error is encountered the job never starts again.

Any ideas ?

Change the || in your if statement to && if you're trying to catch only errors 501, 502, and 503. Right now, that if is catching all status codes. And, actually, since you said you're looking specifically for 500 through 503, you need to change the > to >= , as in: if (statusCode >= 500 && statusCode < 504)

Why not use the timer you've already got to manage the sleep:

if (statusCode > 500 || statusCode < 504)
{
    aTimer.Interval = 600000;
    return;
}

Then, within your timer callback code, you can check whether normal operations have resumed and reset the Interval back to 10000 .

Add these variables in your class.

static bool IsErrorWait = false;
static double DefaultTick = 10000;
static double WaitTick = 600000;

Change your static method as shown below

private static void SetTimer()
{
    aTimer = new System.Timers.Timer(DefaultTick);
    aTimer.Elapsed += OnTimedEvent;
    aTimer.AutoReset = true;
    aTimer.Enabled = true;
}

Then add this code in the Timer Event.

if (IsErrorWait == true)
{
    aTimer.Interval = DefaultTick;
    IsErrorWait = false;
}

if (statusCode >= 500 && statusCode <= 504)
{
    IsErrorWait = true;
    aTimer.Interval = WaitTick;
    return;
}
else
{
    // This will post the migration status for anything EXCEPT a  server failure
    PostMigrationStatus(DocEntry, JobSuccess, jobID);
}

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