我正在使用计时器,并已将线程作为解决问题的第一步。我必须每 30 秒发送一次参考,每次单击按钮时都会创建此参考。所以我有以下代码:
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);
}
以下是 MyReference 类:
public class MyReference()
{
public string Id {get;set;}
public string FullName {get;set;}
}
编辑问题重点:
问题是它只发送最新的值someReference
而忽略以前的值。如何在 30 秒后发送每个值而不用 someReference 的最新值替换它们?