Added the ability to prevent cancellation.
This commit is contained in:
parent
1e5f0dba4e
commit
a7ff0a7ab4
12
SrcMod/Shell/Modules/ObjectModels/CanCancelAttribute.cs
Normal file
12
SrcMod/Shell/Modules/ObjectModels/CanCancelAttribute.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace SrcMod.Shell.Modules.ObjectModels;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
|
||||||
|
public class CanCancelAttribute : Attribute
|
||||||
|
{
|
||||||
|
public readonly bool CanCancel;
|
||||||
|
|
||||||
|
public CanCancelAttribute(bool canCancel)
|
||||||
|
{
|
||||||
|
CanCancel = canCancel;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
public class CommandInfo
|
public class CommandInfo
|
||||||
{
|
{
|
||||||
|
public bool CanBeCancelled { get; private set; }
|
||||||
public required ModuleInfo Module { get; init; }
|
public required ModuleInfo Module { get; init; }
|
||||||
public required MethodInfo Method { get; init; }
|
public required MethodInfo Method { get; init; }
|
||||||
public string Name { get; private set; }
|
public string Name { get; private set; }
|
||||||
@ -11,6 +12,7 @@ public class CommandInfo
|
|||||||
|
|
||||||
private CommandInfo()
|
private CommandInfo()
|
||||||
{
|
{
|
||||||
|
CanBeCancelled = false;
|
||||||
Name = string.Empty;
|
Name = string.Empty;
|
||||||
NameId = string.Empty;
|
NameId = string.Empty;
|
||||||
Parameters = Array.Empty<ParameterInfo>();
|
Parameters = Array.Empty<ParameterInfo>();
|
||||||
@ -34,10 +36,13 @@ public class CommandInfo
|
|||||||
CommandAttribute[] attributes = info.GetCustomAttributes<CommandAttribute>().ToArray();
|
CommandAttribute[] attributes = info.GetCustomAttributes<CommandAttribute>().ToArray();
|
||||||
if (attributes.Length <= 0) return Array.Empty<CommandInfo>();
|
if (attributes.Length <= 0) return Array.Empty<CommandInfo>();
|
||||||
|
|
||||||
|
CanCancelAttribute? cancel = info.GetCustomAttribute<CanCancelAttribute>();
|
||||||
|
|
||||||
foreach (CommandAttribute attribute in attributes)
|
foreach (CommandAttribute attribute in attributes)
|
||||||
{
|
{
|
||||||
commands.Add(new()
|
commands.Add(new()
|
||||||
{
|
{
|
||||||
|
CanBeCancelled = cancel is null || cancel.CanCancel,
|
||||||
Method = info,
|
Method = info,
|
||||||
Module = parentModule,
|
Module = parentModule,
|
||||||
Name = info.Name,
|
Name = info.Name,
|
||||||
|
|||||||
@ -241,7 +241,7 @@ public class Shell
|
|||||||
activeCommand.DoWork += runCommand;
|
activeCommand.DoWork += runCommand;
|
||||||
activeCommand.RunWorkerAsync();
|
activeCommand.RunWorkerAsync();
|
||||||
|
|
||||||
activeCommand.WorkerSupportsCancellation = true;
|
activeCommand.WorkerSupportsCancellation = command.CanBeCancelled;
|
||||||
|
|
||||||
while (activeCommand is not null && activeCommand.IsBusy) Thread.Yield();
|
while (activeCommand is not null && activeCommand.IsBusy) Thread.Yield();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user