Add Service<Framework>.RunOnTick() (#832)

This commit is contained in:
kizer 2022-05-12 17:36:05 +09:00 committed by GitHub
parent bf1a525e4c
commit 9413755ee3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 237 additions and 6 deletions

View file

@ -114,6 +114,9 @@ namespace Dalamud.Interface.Internal.Windows
private DtrBarEntry? dtrTest2;
private DtrBarEntry? dtrTest3;
// Task Scheduler
private CancellationTokenSource taskSchedCancelSource = new();
private uint copyButtonIndex = 0;
/// <summary>
@ -1359,6 +1362,15 @@ namespace Dalamud.Interface.Internal.Windows
ImGuiHelpers.ScaledDummy(10);
ImGui.SameLine();
if (ImGui.Button("Cancel using CancellationTokenSource"))
{
this.taskSchedCancelSource.Cancel();
this.taskSchedCancelSource = new();
}
ImGui.Text("Run in any thread: ");
ImGui.SameLine();
if (ImGui.Button("Short Task.Run"))
{
Task.Run(() => { Thread.Sleep(500); });
@ -1368,7 +1380,8 @@ namespace Dalamud.Interface.Internal.Windows
if (ImGui.Button("Task in task(Delay)"))
{
Task.Run(async () => await this.TestTaskInTaskDelay());
var token = this.taskSchedCancelSource.Token;
Task.Run(async () => await this.TestTaskInTaskDelay(token));
}
ImGui.SameLine();
@ -1391,28 +1404,71 @@ namespace Dalamud.Interface.Internal.Windows
});
}
ImGui.Text("Run in Framework.Update: ");
ImGui.SameLine();
if (ImGui.Button("ASAP"))
{
Task.Run(async () => await Service<Framework>.Get().RunOnTick(() => { }, cancellationToken: this.taskSchedCancelSource.Token));
}
ImGui.SameLine();
if (ImGui.Button("In 1s"))
{
Task.Run(async () => await Service<Framework>.Get().RunOnTick(() => { }, cancellationToken: this.taskSchedCancelSource.Token, delay: TimeSpan.FromSeconds(1)));
}
ImGui.SameLine();
if (ImGui.Button("In 60f"))
{
Task.Run(async () => await Service<Framework>.Get().RunOnTick(() => { }, cancellationToken: this.taskSchedCancelSource.Token, delayTicks: 60));
}
ImGui.SameLine();
if (ImGui.Button("Error in 1s"))
{
Task.Run(async () => await Service<Framework>.Get().RunOnTick(() => throw new Exception("Test Exception"), cancellationToken: this.taskSchedCancelSource.Token, delay: TimeSpan.FromSeconds(1)));
}
ImGui.SameLine();
if (ImGui.Button("As long as it's in Framework Thread"))
{
Task.Run(async () => await Service<Framework>.Get().RunOnFrameworkThread(() => { Log.Information("Task dispatched from non-framework.update thread"); }));
Service<Framework>.Get().RunOnFrameworkThread(() => { Log.Information("Task dispatched from framework.update thread"); }).Wait();
}
if (ImGui.Button("Drown in tasks"))
{
var token = this.taskSchedCancelSource.Token;
Task.Run(() =>
{
for (var i = 0; i < 100; i++)
{
token.ThrowIfCancellationRequested();
Task.Run(() =>
{
for (var i = 0; i < 100; i++)
{
token.ThrowIfCancellationRequested();
Task.Run(() =>
{
for (var i = 0; i < 100; i++)
{
token.ThrowIfCancellationRequested();
Task.Run(() =>
{
for (var i = 0; i < 100; i++)
{
Task.Run(() =>
token.ThrowIfCancellationRequested();
Task.Run(async () =>
{
for (var i = 0; i < 100; i++)
{
token.ThrowIfCancellationRequested();
Thread.Sleep(1);
}
});
@ -1652,9 +1708,9 @@ namespace Dalamud.Interface.Internal.Windows
}
}
private async Task TestTaskInTaskDelay()
private async Task TestTaskInTaskDelay(CancellationToken token)
{
await Task.Delay(5000);
await Task.Delay(5000, token);
}
#pragma warning disable 1998