Less CPU usage by creating tasks per action, instead of thread
This commit is contained in:
parent
74d30ba1b9
commit
0b98638cd5
@ -1,5 +1,6 @@
|
|||||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CUsers_005CGlax_005CRiderProjects_005CGlaxLogger_005CGlaxLogger_005Cbin_005CDebug_005Cnet7_002E0_005CGlaxLogger_002Edll/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CUsers_005CGlax_005CRiderProjects_005CGlaxLogger_005CGlaxLogger_005Cbin_005CDebug_005Cnet7_002E0_005CGlaxLogger_002Edll/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AHttpClient_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc439425da351c75ac7d966a1cc8324b51a9c471865af79d2f2f3fcb65e392_003FHttpClient_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||||
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue"><AssemblyExplorer>
|
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue"><AssemblyExplorer>
|
||||||
<Assembly Path="C:\Users\Glax\RiderProjects\GlaxLogger\GlaxLogger\bin\Debug\net7.0\GlaxLogger.dll" />
|
<Assembly Path="C:\Users\Glax\RiderProjects\GlaxLogger\GlaxLogger\bin\Debug\net7.0\GlaxLogger.dll" />
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<Authors>Glax</Authors>
|
<Authors>Glax</Authors>
|
||||||
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<Version>3.0.2</Version>
|
<Version>3.1.0</Version>
|
||||||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
||||||
<LangVersion>latestmajor</LangVersion>
|
<LangVersion>latestmajor</LangVersion>
|
||||||
<PackageProjectUrl>https://github.com/C9Glax/CShocker</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/C9Glax/CShocker</PackageProjectUrl>
|
||||||
|
@ -10,10 +10,9 @@ public abstract class Api : IDisposable
|
|||||||
// ReSharper disable 4 times MemberCanBePrivate.Global -> Exposed
|
// ReSharper disable 4 times MemberCanBePrivate.Global -> Exposed
|
||||||
protected ILogger? Logger;
|
protected ILogger? Logger;
|
||||||
public readonly DeviceApi ApiType;
|
public readonly DeviceApi ApiType;
|
||||||
private readonly Queue<ValueTuple<ControlAction, Shocker, int, int>> _queue = new();
|
private Queue<DateTime> order = new();
|
||||||
private bool _workOnQueue = true;
|
private Dictionary<DateTime, Task> tasks = new();
|
||||||
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
|
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
|
||||||
private readonly Thread _workQueueThread;
|
|
||||||
private const short CommandDelay = 50;
|
private const short CommandDelay = 50;
|
||||||
internal readonly IntegerRange ValidIntensityRange, ValidDurationRange;
|
internal readonly IntegerRange ValidIntensityRange, ValidDurationRange;
|
||||||
|
|
||||||
@ -38,7 +37,12 @@ public abstract class Api : IDisposable
|
|||||||
foreach (Shocker shocker in shockers)
|
foreach (Shocker shocker in shockers)
|
||||||
{
|
{
|
||||||
this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} Intensity: {intensity} Duration: {duration}\nShocker:\n{shocker}");
|
this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} Intensity: {intensity} Duration: {duration}\nShocker:\n{shocker}");
|
||||||
_queue.Enqueue(new(action, shocker, intensity, duration));
|
ValueTuple<ControlAction, Shocker, int, int> tuple = new(action, shocker, intensity, duration);
|
||||||
|
DateTime now = DateTime.Now;
|
||||||
|
Task t = new (() => ExecuteTask(now, tuple));
|
||||||
|
order.Enqueue(now);
|
||||||
|
tasks.Add(now, t);
|
||||||
|
t.Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,19 +54,17 @@ public abstract class Api : IDisposable
|
|||||||
this.Logger = logger;
|
this.Logger = logger;
|
||||||
this.ValidIntensityRange = validIntensityRange;
|
this.ValidIntensityRange = validIntensityRange;
|
||||||
this.ValidDurationRange = validDurationRange;
|
this.ValidDurationRange = validDurationRange;
|
||||||
this._workQueueThread = new Thread(QueueThread);
|
|
||||||
this._workQueueThread.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void QueueThread()
|
private void ExecuteTask(DateTime when, ValueTuple<ControlAction, Shocker, int, int> tuple)
|
||||||
{
|
{
|
||||||
while (_workOnQueue)
|
while (order.First() != when)
|
||||||
if (_queue.Count > 0 && _queue.Dequeue() is { } action)
|
Thread.Sleep(CommandDelay);
|
||||||
{
|
this.Logger?.Log(LogLevel.Information, $"Executing: {Enum.GetName(tuple.Item1)} Intensity: {tuple.Item3} Duration: {tuple.Item4}\nShocker:\n{tuple.Item2}");
|
||||||
this.Logger?.Log(LogLevel.Information, $"Executing: {Enum.GetName(action.Item1)} Intensity: {action.Item3} Duration: {action.Item4}\nShocker:\n{action.Item2}");
|
ControlInternal(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4);
|
||||||
ControlInternal(action.Item1, action.Item2, action.Item3, action.Item4);
|
Thread.Sleep(tuple.Item4);
|
||||||
Thread.Sleep(action.Item4 + CommandDelay);
|
tasks.Remove(when);
|
||||||
}
|
order.Dequeue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetLogger(ILogger? logger)
|
public void SetLogger(ILogger? logger)
|
||||||
@ -92,6 +94,7 @@ public abstract class Api : IDisposable
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_workOnQueue = false;
|
foreach ((DateTime when, Task? task) in tasks)
|
||||||
|
task?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user