0

I am trying to set up a timer and for some reason I keep getting errors when I try to recursively call ProcessStateChangedQueue(). I originally tried using Timer instead of DispacherTimer, but this is a WPF application. I could have done timer.elapsed if I used timer, but can't in this case. Seems as though I have to use timer.tick. I have never used this before. You can see where timer.elapsed is commented out, I just need to fix this to where I can recursively call ProcessStateChangedQueue() as coded below. The only error is No overload for "ProcessStateChangedQueue" matches delegate "System.EventHandler" Maybe I should not use EventHandler. I am just not sure... any ideas?

#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;
}
developerME
  • 165
  • 1
  • 1
  • 7
  • There are numerous questions on Stack Overflow which already address this type of error. See e.g. the marked duplicates above. Note that in your case, you could also "adapt" the method using an anonymous method: `timer.Tick += (sender, e) => ProcessStateChangedQueue();` – Peter Duniho Mar 24 '17 at 21:22
  • None of those so called duplicates are the same issue I have. They may have the same error, but the way to solve it is completely different then what they have. And if it isn't I don't know what I need to do. Why can't anyone on stack overflow ever actually help. I don't get it. Thanks. Looks like I will have to repost over and over again until I can get a helpful response. – developerME Mar 24 '17 at 23:11
  • _"the way to solve it is completely different then what they have"_ -- no. the way to solve it for your code is _exactly_ the same as what they have. You _must_ provide an event handler method that has the exact signature required by the delegate type. Whether that method has a name (as in the marked duplicates) or it is anonymous (as in the suggestion in my comment), the solution is _always_ the same: provide a suitable method, instead of trying to provide an unsuitable one. – Peter Duniho Mar 25 '17 at 07:10

0 Answers0