I am working with timers and have implemented threading as first step in solving my problem. I have to send some reference every 30 seconds and this reference is created everytime a button is clicked. So I have the following code:
public MyReference SomeReference {get;set;}
public string MyFullName {get;set;} //this is bound to WPF textbox
public bool Saved()
{
SomeReference.Id = GeneratedId;
SomeReference.FullName = MyFullName;
return SavedtoDB(SomeReference);
}
public void OnButtonClick()
{
if(Saved()) //timer should only start if it is already saved in the database
var t = new Thread(() => ExecuteTimer(SomeReference));
}
public void ExecuteTimer(MyReference someReference)
{
System.Timers.Timer sendTimer = new System.Timers.Timer(30000);
sendTimer.Elapsed += (sender, e) => ExecuteElapsed(sender, e, someReference, sendTimer);
sendTimer.Start();
}
public void ExecuteElapsed(object source, ElapsedEventArgs e, MyReference someReference, System.Timers.Timer sendtimer)
{
sendtimer.Stop();
Send(someReference);
}
The following is the MyReference class:
public class MyReference()
{
public string Id {get;set;}
public string FullName {get;set;}
}
Edit for problem emphasis:
The problem is that it only sends the latest values someReference
and disregards the previous ones. How will I send each values after 30 seconds without replacing the them with the latest value of someReference?