我正在尝试设置一个计时器,出于某种原因,当我尝试递归调用ProcessStateChangedQueue()
时,我不断收到错误。我最初尝试使用Timer
而不是DispacherTimer
,但这是一个WPF应用程序。如果我使用计时器,我本可以完成timer.elapsed,但在这种情况下不能。好像我必须使用timer.tick。我以前从未用过这个。您可以看到timer.elapsed被注释掉的地方,我只需要将其修复到我可以递归调用ProcessStateChangedQueue()
的位置,如下所示。唯一的错误是No overload for "ProcessStateChangedQueue" matches delegate "System.EventHandler"
也许我不应该使用EventHandler
。我只是不确定......有什么想法吗?
#region ********** STATE **********
public delegate void MapStateChangedHandler(object sender, MapStateEventArgs e);
public event MapStateChangedHandler MapStateChanged;
private Queue<String> stateChangedQueue = new Queue<String>();
private bool processingStateChanged = false;
private void RaiseMapStateChanged(string property)
{
if (!CanSaveState) return;
RebuildState();
if (MapStateChanged != null)
{
//MapStateChanged(this, new MapStateEventArgs(property, State));
stateChangedQueue.Enqueue(property);
if (!processingStateChanged)
{
ProcessStateChangedQueue();
}
}
}
private void ProcessStateChangedQueue()
{
if (stateChangedQueue.Count != 0)
{
processingStateChanged = true;
MapStateChanged(this, new MapStateEventArgs(stateChangedQueue.Dequeue(), State));
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
//timer.Elapsed = timer.Elapsed + new ElapsedEventHandler(ProcessStateChangedQueue);
timer.Tick += new EventHandler(ProcessStateChangedQueue);
timer.Start();
}
else
{
processingStateChanged = false;
}
}
private void RebuildState()
{
State.IsMonochrome = Monochrome;
State.SelectedMapIndex = MapTileLayers.IndexOf(SelectedBaseMap);
Dictionary<int, double> overlays = new Dictionary<int, double>();
foreach (MapOverlay overlay in MapOverlays)
{
if (overlay.IsVisible)
overlays.Add(MapOverlays.IndexOf(overlay), overlay.Opacity);
}
State.MapOverlays = overlays;
State.ContrastValue = Contrast;
}