Ryujinx-GtkSharp/Source/Samples/Sections/Miscellaneous/TimerSection.cs
zii-dmg 0c5bd3f471
Fixed glib source double removal (#327)
Fixed glib source double removal then using Source.Remove (Timeout.Remove, Idle.Remove).
I don't know if fix is correct or safe, but it solves problem that you can test in TimerDemo section from samples.

Repro:
If on Windows you should enable console in samples: <OutputType>Exe</OutputType>
Go to TimerDemo section and press buttons:

1. Add timer
2. Remove timer by handler
3. GC - no error in console

1. Add timer
2. Remove timer
3. GC - error in console "GLib-CRITICAL **: 20:29:41.579: Source ID 123 was not found when attempting to remove it"
2022-01-27 21:49:10 +01:00

85 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using Gtk;
namespace Samples
{
[Section(ContentType = typeof(TimerDemo), Category = Category.Miscellaneous)]
class TimerSection : Box
{
public TimerSection() : base(Orientation.Horizontal, 3)
{
Valign = Align.Start;
TimerDemo.Create(this);
}
}
static class TimerDemo
{
public static void Create(Box box)
{
List<uint> timers = new List<uint>();
bool removeByHandler = false;
var btnAddTimer = new Button() { Label = "Add timer" };
btnAddTimer.Clicked += delegate
{
uint id = 0;
id = GLib.Timeout.Add(500, () =>
{
ApplicationOutput.WriteLine("Timer tick " + id);
if (removeByHandler)
{
removeByHandler = false;
timers.Remove(id);
ApplicationOutput.WriteLine("Remove timer from handler " + id);
return false;
}
return true;
});
timers.Add(id);
ApplicationOutput.WriteLine("Add timer " + id);
};
var btnRemoveTimer = new Button() { Label = "Remove timer" };
btnRemoveTimer.Clicked += delegate
{
if (timers.Count == 0)
return;
uint id = timers[0];
timers.RemoveAt(0);
GLib.Timeout.Remove(id);
ApplicationOutput.WriteLine("Remove timer " + id);
};
var btnRemoveTimerByHandler = new Button() { Label = "Remove timer by handler" };
btnRemoveTimerByHandler.Clicked += delegate
{
if (timers.Count == 0)
return;
removeByHandler = true;
ApplicationOutput.WriteLine("Remove timer by handler");
};
var btnGc = new Button() { Label = "GC" };
btnGc.Clicked += delegate
{
ApplicationOutput.WriteLine("GC");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
};
box.PackStart(btnAddTimer, false, false, 0);
box.PackStart(btnRemoveTimer, false, false, 0);
box.PackStart(btnRemoveTimerByHandler, false, false, 0);
box.PackStart(btnGc, false, false, 0);
}
}
}