Terminal.Gui: MainLoop.RunTimers() method throws System.ArgumentException after few hours.

Can someone explain me why I’m getting this error after around 8 hours of running? Terminal.Gui version 1.4

AmiAI - System.ArgumentException: An item with the same key has already been added. Key: 637865266986268040 (Parameter "key") at System.Collections.Generic.SortedList"2.Add(TKey key, TValue value) at Terminal.Gui.MainLoop.RunTimers() at Terminal.Gui.MainLoop.MainIteration() at Terminal.Gui.Application.RunLoop(RunState state, Boolean wait) at Terminal.Gui.Application.Run(Toplevel view, Func"2 errorHandler) at Terminal.Gui.Application.Run(Func"2 errorHandler) at Pai.Ama.Run() at Pai.Program.Main(String[] args)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23

Most upvoted comments

@tznind your code modification fixed the issue on MacOS. No more problems during 23 hours.

But at first sight it mainly do the same thing as original but using a method.

RunTimers has an if/else statement in it. The ‘if’ condition uses AddTimer but the ‘else’ just directly added to the dictionary. My PR adds the collision dodging to that else statement not just a new method.

foreach (var t in copy) {
		var k = t.Key;
		var timeout = t.Value;
		if (k < now) {
			if (timeout.Callback (this))
				AddTimeout (timeout.Span, timeout); // has the collision dodging code
		} else {
			lock (timeoutsLockToken) {
				timeouts.Add (k, timeout); // does not have the collision dodging code
			}
		}
	}

Old code (prior to PR)

After changing the code and starting timers via Application.Mainloop.Invoke(), the program didn’t throw any exceptions after 24 hours of running. OS: Windows, now testing MacOS.

Just done that. Now, waiting.

@BDisp checked, again same error.

No, I’m using AddTimeout function directly and call necessary actions inside bool function. I think this is why I’m getting Exception. Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(1000), timer);