-2

好的,所以我有一个程序,通过比较某个字符串与我用作参考的“临时”字符串不同,每当有人新关注频道时,它都会检查 twitch url。但不是每次字符串不同时只输出一条消息,而是卡在输出最新的追随者,然后是第二个最新的追随者,然后是最新的追随者的循环中,等等。

我错过了什么?此外,是否有更好的方法来检查某个字符串是否已更新?

        private void DonationListen()
    {
        try
        {
            followers = this.donationClient.DownloadString("https://api.twitch.tv/kraken/channels/" + channel.Trim() + "/follows");

            donationTimer.Interval = 10000;
            donationTimer.Elapsed += new ElapsedEventHandler(CheckUpdates);
            donationTimer.Start();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private void CheckUpdates(object source, ElapsedEventArgs e)
    {
        donationTimer.Stop();

        int startIndex = followers.IndexOf("display_name\":") + 15;
        int endIndex = followers.IndexOf(",\"logo", startIndex);
        prevFollower = followers.Substring(startIndex, (endIndex - 1) - startIndex);

        if (firstRun == true)
        {
            temp = prevFollower;
        }
        else if (prevFollower != temp)
        {
            //New follower detected
            temp = prevFollower;
            if (updateFollower != null)
            {
                updateFollower(prevFollower);
            }
        }
        else
        {
            //Follower is the same as before
        }

        firstRun = false;
        DonationListen();
    }

我认为这可能与尝试从 url 获取新字符串的下载字符串有关,但由于它当前正在更新而失败,因此 CheckUpdates 没有正确的信息或其他什么?

4

1 回答 1

1

如果没有好的代码示例,很难确定问题出在哪里。因此,我们将检查您向我们展示的代码。

基于此,在我看来,您的“循环”似乎是由重复订阅同一事件引起的。

在您的DonationListen()方法中,您有以下声明:

donationTimer.Elapsed += new ElapsedEventHandler(CheckUpdates);

CheckUpdates()方法(即您订阅的​​处理程序)中,您有以下语句(作为最后一条语句):

DonationListen();

换句话说,每次Elapsed引发计时器的事件时,您都会向该事件添加另一个事件处理程序实例。对于您添加的每个处理程序,CheckUpdates()都会调用该方法。

同样,如果没有好的代码示例,很难确定最好的解决方案是什么。但是鉴于这里的代码,在我看来,您可以从CheckUpdates()方法中删除最后一条语句,因为该DonationListen()方法似乎没有做任何需要再次做的事情。

于 2015-01-21T19:01:31.057 回答