定时器每1秒钟复位一次

时间:2017-11-12 21:19:47

标签: c# winforms timer

我试图每1秒重置一次计时器,以便从webscript中检索响应。但我尝试过重置定时器的所有内容,如启动和停止功能,以及处理定时器和创建新定时器。它只是找到第一个响应,然后再不查询它

它的工作方式是:

当按下书上的按钮时>启动计时器>计时器命中0>查询网络链接>将响应放在标签4中(直到这里工作)>时间重置>查询网络链接>将响应放在标签4中。它将继续执行此操作,直到按下预订关闭按钮

代码:

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

1 个答案:

答案 0 :(得分:2)

您的代码中存在一个基本问题:计时器自动运行其tick事件,直到您告诉它停止。你告诉它在第一次打勾后停止并错误地创建一个新的。你可以完全删除它:

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

此外,由于您使用using statements,这也毫无意义:

request.GetResponse().Dispose();

并且HttpWebRequestHttpWebResponse基本上已弃用,请改用HttpClient

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;

        }
    }
}

您可能还想阅读switch语句,删除所有if个。