简体   繁体   中英

C# Timer get static method with parameters?

I have following function to get yahoo weater rss items. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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