簡體   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