KjayCopper
KjayCopper

Reputation: 27

Making a Timer reset every 1 second

I am trying to reset the timer every 1 second so that it retrieves a response from the webscript. but everything I have tried with resetting timers Like start and Stop functions as well as disposing the timer and creating a new one. it just finds the first response then does not query it again

The way it should work is:

when the book on button is pressed > Start timer > timer hits 0 > query web link > put response in Label 4 (works up until here) > time reset > query web link > put response in Label 4. it would continue to do this until the book off button is pressed

Code:

private void timer1_Tick(object sender, EventArgs e)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://fms.psrpc.co.uk/apistate.php?" + ApiKey);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (response)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            if (reader.ReadToEnd() == "State 1")
            {
                label4.Text = "On Duty";
                label4.ForeColor = Color.Gray;

            }
            else
            if (reader.ReadToEnd() == "State 2")
            {
                label4.Text = "Available for calls";
                label4.ForeColor = Color.Green;

            }
            else
            if (reader.ReadToEnd() == "State 4")
            {
                label4.Text = "On Break";
                label4.ForeColor = Color.Yellow;

            }
            else
            if (reader.ReadToEnd() == "State 5")
            {
                label4.Text = "Responding to call";
                label4.ForeColor = Color.Orange;

            }
            else
            if (reader.ReadToEnd() == "State 6")
            {
                label4.Text = "On Scene";
                label4.ForeColor = Color.LightBlue;

            }
            else
            if (reader.ReadToEnd() == "State 7")
            {
                label4.Text = "Traffic Stop";
                label4.ForeColor = Color.Purple;

            }
            else
            if (reader.ReadToEnd() == "PANIC")
            {
                label4.Text = "PANIC BUTTON ACTIVATED";
                label4.ForeColor = Color.Red;

            }
            else
            if (reader.ReadToEnd() == "Assigned")
            {
                label4.Text = "Assigned to call";
                label4.ForeColor = Color.Brown;

            }



        }
        request.GetResponse().Dispose();

        timer1.Dispose();
        timer1 = new System.Windows.Forms.Timer();
    }

Upvotes: 0

Views: 231

Answers (1)

Camilo Terevinto
Camilo Terevinto

Reputation: 32072

There is a fundamental issue in your code: a timer runs its tick event automatically, until you tell it to stop. You are telling it to stop after the first tick and erroneously creating a new one. You can remove this completely:

timer1.Dispose();
timer1 = new System.Windows.Forms.Timer();

Also, since you are using using statements, this is also pointless:

request.GetResponse().Dispose();

And, HttpWebRequest and HttpWebResponse are basically deprecated, use HttpClient instead:

private void timer1_Tick(object sender, EventArgs e)
{
    using (var client = new HttpClient())
    { 
        string data = client.GetStringAsync("http://fms.psrpc.co.uk/apistate.php?" + ApiKey).GetAwaiter().GetResult();

        if (data == "State 1")
        {
            label4.Text = "On Duty";
            label4.ForeColor = Color.Gray;

        }
        else
        if (data  == "State 2")
        {
            label4.Text = "Available for calls";
            label4.ForeColor = Color.Green;

        }
        else
        if (data == "State 4")
        {
            label4.Text = "On Break";
            label4.ForeColor = Color.Yellow;

        }
        else
        if (data == "State 5")
        {
            label4.Text = "Responding to call";
            label4.ForeColor = Color.Orange;

        }
        else
        if (data == "State 6")
        {
            label4.Text = "On Scene";
            label4.ForeColor = Color.LightBlue;

        }
        else
        if (data == "State 7")
        {
            label4.Text = "Traffic Stop";
            label4.ForeColor = Color.Purple;

        }
        else
        if (data == "PANIC")
        {
            label4.Text = "PANIC BUTTON ACTIVATED";
            label4.ForeColor = Color.Red;

        }
        else
        if (data == "Assigned")
        {
            label4.Text = "Assigned to call";
            label4.ForeColor = Color.Brown;

        }
    }
}

You might also want to read up on the switch statement to remove all those ifs.

Upvotes: 2

Related Questions