C# - Timer.Stop()不起作用

时间:2016-11-23 19:47:02

标签: c#

我通过客户端拨打电话来启动StopTickerTimer函数,该函数成功调用StopTickerTimer函数,但遗憾的是并没有实际停止计时器。计时器启动并正常工作,但我似乎无法理解为什么我的计时器不能停止。我试过设置timer.Enabled = false。我尝试过各种我在这里找到的解决方案,但似乎没有任何效果。

    public Timer timer = new Timer();
    private string jsonContents;
    private string currentTickerPlaylist;
    private int i = 0;
    private List<string> playlistTickers;

    public void StartTickerTimer(int seconds, string selectedPlaylist)
    {
        currentTickerPlaylist = selectedPlaylist;
        InitPlaylistTickerTimer(seconds);            
    }

    public void StopTickerTimer()
    {
        StopTimer();
    }

    private void InitPlaylistTickerTimer(int seconds)
    {
        timer.Elapsed += new ElapsedEventHandler(t_Tick);
        timer.Interval = seconds * 1000;
        timer.AutoReset = true;
        timer.Start();
    }

    private void StopTimer()
    {
        timer.Stop();
    }

    private void t_Tick(object sender, EventArgs e)
    {

        SetPlaylistTickerSettings();
        if (i >= playlistTickers.Count)
        {
            i = 0;
            Payload ticker = new Payload
            {
                ticker = playlistTickers[i]
            };
            Payload ticker2 = new Payload
            {
                payload = ticker
            };
            var json = ServiceStack.Text.JsonSerializer.SerializeToString(ticker2);
            PutToSingular("url", json);
            i++;
        }
        else
        {
            Payload ticker = new Payload
            {
                ticker = playlistTickers[i]
            };
            Payload ticker2 = new Payload
            {
                payload = ticker
            };
            var json = ServiceStack.Text.JsonSerializer.SerializeToString(ticker2);
            PutToSingular("url", json);
            i++;
        }

    }

1 个答案:

答案 0 :(得分:-1)

你启动了计时器,但你没有打电话给你的#34; StopTimer&#34;代码中的任何地方......