mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-11 18:44:36 +01:00
Add Service<Framework>.RunOnTick() (#832)
This commit is contained in:
parent
bf1a525e4c
commit
9413755ee3
2 changed files with 237 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue