繁体   English   中英

C#定时器获取参数的静态方法?

[英]C# Timer get static method with parameters?

我有以下功能来获取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();
    }
}

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

谢谢。

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

范例:

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")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM