C#定时器获取静态参数方法?
我有以下功能来获取yahoo weater rss项目。我想在特定的时间间隔内运行此方法C#定时器获取静态参数方法?,c#,timer,setinterval,C#,Timer,Setinterval,我有以下功能来获取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 f
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();
}
}
但这是错误的方式。(错误:方法名expexted)。我该怎么做?它不一定是计时器。可能还有另一种方法可以在特定的时间间隔内调用此函数
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();
}
谢谢。错误是明确的。您必须提供方法名称,而不是操作 例如:
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")
}
}
错误是明确的。您必须提供方法名称,而不是操作 例如:
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")
}
}
浏览本页:浏览本页: