SharpLibEar/Action.cs
author StephaneLenclud
Wed, 31 Aug 2016 17:28:30 +0200
changeset 264 4a08e1b7ba64
parent 258 e237c2e33545
child 265 82e87f4956ea
permissions -rw-r--r--
EAR: Actions now support multiple iterations.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Diagnostics;
     7 using System.Runtime.Serialization;
     8 using System.Threading;
     9 using System.Threading.Tasks;
    10 
    11 namespace SharpLib.Ear
    12 {
    13     [DataContract]
    14     public abstract class Action: Object
    15     {
    16         [DataMember]
    17         [AttributeObjectProperty
    18             (
    19             Id = "Action.Iterations",
    20             Name = "Iterations",
    21             Description = "Specifies the number of time this action should execute.",
    22             Minimum = "0",
    23             Maximum = "10000",
    24             Increment = "1"
    25             )
    26         ]
    27         public int Iterations { get; set; } = 1;
    28 
    29         /// <summary>
    30         /// 
    31         /// </summary>
    32         public bool Enabled
    33         {
    34             get { return Iterations > 0; }
    35         }
    36 
    37         protected abstract Task DoExecute();
    38 
    39         /// <summary>
    40         /// Allows testing from generic edit dialog.
    41         /// </summary>
    42         public void Test()
    43         {
    44             Trace.WriteLine("Action test");
    45             Execute();
    46         }
    47 
    48         /// <summary>
    49         /// Execute our action N times.
    50         /// </summary>
    51         /// <returns></returns>
    52         public async Task Execute()
    53         {
    54             if (!IsValid())
    55             {
    56                 Trace.WriteLine("EAR: Action.Execute: WARNING: Action invalid, aborting execution: " + Brief());
    57                 return;
    58             }
    59 
    60             if (!Enabled)
    61             {
    62                 Trace.WriteLine("EAR: Action.Execute: Action disabled: " + Brief());
    63                 return;
    64             }
    65 
    66             for (int i = Iterations; i > 0; i--)
    67             {
    68                 Trace.WriteLine($"EAR: Action.Execute: [{Iterations - i + 1}/{Iterations}] - {BriefBase()}");
    69                 await DoExecute();
    70             }            
    71         }
    72 
    73 
    74         public virtual string BriefBase()
    75         {
    76             return base.Brief();
    77         }
    78 
    79         /// <summary>
    80         /// Dynamic object description.
    81         /// </summary>
    82         /// <returns></returns>
    83         public sealed override string Brief()
    84         {
    85             return Iterations > 1 ? $"{Iterations} x " + BriefBase():BriefBase();
    86         }
    87 
    88 
    89 
    90 
    91 
    92 
    93     }
    94 
    95 
    96 }