0

我有以下功能来获取 yahoo weater rss 项目。我想运行这个方法特定的时间间隔。

public static Weathers GetYahooWeatherRssItem(string rssUrl, XNamespace yWeatherNS)
{
    XDocument rssXml = XDocument.Load(rssUrl);
    //yWeatherNS = "http://xml.weather.yahoo.com/ns/rss/1.0";

    var feeds = from feed in rssXml.Descendants("item")
                select new Weathers
                {
                    Title = feed.Element("title").Value,
                    Link = feed.Element("link").Value,
                    Description = feed.Element("description").Value,
                    Temp = Int32.Parse(feed.Element(yWeatherNS + "condition").Attribute("temp").Value),
                    Text = feed.Element(yWeatherNS + "condition").Attribute("text").Value,
                    ConditionCode = Int32.Parse(feed.Element(yWeatherNS + "condition").Attribute("code").Value),
                    Date = DateTimeManager.TrimZoneAndParse(feed.Element(yWeatherNS + "condition").Attribute("date").Value)
                    //Latitute = float.Parse(feed.Element(yWeatherNS + "lat").Attribute("latitute").Value),
                    //Longtitute = float.Parse(feed.Element(yWeatherNS + "lon").Attribute("longtitute").Value)
                 };

    return feeds.FirstOrDefault();
}

我试图在 app_start.cs 中做这样的事情:

public static class TimerEventsInitializations
{
    public static void InitializeWeatherReader()
    {
        Timer t = new Timer(1000);
        t.Elapsed += new EventHandler(YahooWeatherManager.GetYahooWeatherRssItem("", "http://xml.weather.yahoo.com/ns/rss/1.0"));
        t.Start();
    }
}

但这是错误的方式。(错误:方法名称已扩展)。我怎样才能做到这一点?它不一定是计时器。可能有另一种方法可以在特定时间间隔内调用此函数。

谢谢。

4

1 回答 1

3

错误是明确的。您必须提供方法名称,而不是操作。

例子 :

public static class TimerEventsInitializations
{
    public static void InitializeWeatherReader()
    {
        Timer t = new Timer(1000);
        t.Elapsed += new EventHandler(MyEventHandler);
        t.Start();
    }

    public static void MyEventHandler()
    {
       YahooWeatherManager.GetYahooWeatherRssItem("", "http://xml.weather.yahoo.com/ns/rss/1.0")
    }
}
于 2012-11-05T09:03:23.270 回答