简体   繁体   English

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

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

I have following function to get yahoo weater rss items. 我有以下功能来获取yahoo weater rss项目。 I want to run this method specific time interval. 我想运行此方法的特定时间间隔。

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

I tried to do something like this in app_start.cs: 我试图在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();
    }
}

But this is the wrong way. 但这是错误的方式。 (Error:Method name expexted). (错误:方法名称已扩展)。 How Can I do this? 我怎样才能做到这一点? It not must be timer. 它不一定是计时器。 there may be another way to do call this function in specific time interval. 在特定时间间隔内可能有另一种方法可以调用此函数。

Thanks. 谢谢。

The error is explicit. 该错误是明确的。 You have to provide a method name, not an action. 您必须提供方法名称,而不是操作。

Example : 范例:

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