Reputation: 43
I have a method in a class that returns the data as List.I need to use timer event for this method to execute in different intervals to check the data.And I need to get the return object from the first method to another method.And I have to call the second method from the Main mehtod in console application.
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();
}
}
I need to get the return data from GetData() method .But timer need not be called in the Main method.How is this posible?
Upvotes: 0
Views: 398
Reputation: 643
put the following on clsSample :
public delegate void EventRaiser(List<string> data);
public event EventRaiser OnDataRetrieved;
and put this on the timer method
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(OnDataRetrieved !=null)
{
OnDataRetrieved(GetData())
}
}
then handle the event from the program.cs class
Upvotes: 1