我在类中有一个方法,它将数据作为List返回。我需要使用timer事件让这个方法以不同的间隔执行来检查数据。我需要从第一个方法获取返回对象到另一个方法。并且我必须在控制台应用程序中调用Main mehtod中的第二个方法。
public class clsSample
{
private static List<string> GetData()
{
data = clsApp.LoadData();
return data;
}
public static void InitTimer()
{
Timer t = new Timer();
t.Elapsed += new ElapsedEventHandler(OnTimedEvent);
t.Interval = 50000;
t.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
GetData();
}
}
class Program
{
static void Main()
{
List<string> data = clsSample.GetData();
}
}
我需要从GetData()方法获取返回数据.But定时器不需要在Main方法中调用。这个怎么可能?
将以下内容放在clsSample上:
public delegate void EventRaiser(List<string> data);
public event EventRaiser OnDataRetrieved;
并把它放在计时器方法上
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(OnDataRetrieved !=null)
{
OnDataRetrieved(GetData())
}
}
然后从program.cs类处理事件