Skip to content

C# Developer Reference

saucepleez edited this page Jun 4, 2018 · 2 revisions

Namespaces and Descriptions

Namespace Description
taskt.Core Manages Automation Commands and underlying core scripting components
taskt.UI Manages UI Forms and UI controls/classes
taskt.Core
taskt.Core.AutomationCommands Contains the automation command definitions
taskt.Core.AutomationCommands.Attributes.ClassAttributes Contains the definitions for Class Attributes
taskt.Core.AutomationCommands.Attributes.PropertyAttributes Contains the definitions for Property Attributes
taskt.Core.Script Contains classes and methods for serializing/deserializing scripts
taskt.UI
taskt.UI.CustomControls Contains definitions for custom controls used by the application (needs refactoring)
taskt.UI.Forms Contains definitions for the main application forms
taskt.UI.Forms.Supplemental Contains definitions for the supplemental application forms

Automation Commands

This project uses reflection in order to display and run the commands. Each Automation Command is a class within the taskt.Core.AutomationCommands namespace which contains the required properties and encapsulated code that the application uses to execute the command. Each Automation Command inherits from an abstract base class for consistency.

Base Class:

 [Serializable]
    public abstract class ScriptCommand
    {
        [XmlAttribute]
        public string CommandName { get; set; }
        [XmlAttribute]
        public bool IsCommented { get; set; }
        [XmlAttribute]
        public string SelectionName { get; set; }
        [XmlAttribute]
        public int DefaultPause { get; set; }
        [XmlAttribute]
        public int LineNumber { get; set; }
        [XmlAttribute]
        public bool PauseBeforeExeucution { get; set; }
        [XmlIgnore]
        public System.Drawing.Color DisplayForeColor { get; set; }
        [XmlAttribute]
        [Attributes.PropertyAttributes.PropertyDescription("Comment Field (Optional)")]
        public string v_Comment { get; set; }
        [XmlAttribute]
        public bool CommandEnabled {  get; set; }

        public ScriptCommand()
        {
            this.DisplayForeColor = System.Drawing.Color.Black;
            this.CommandEnabled = false;
            this.DefaultPause = 250;
            this.IsCommented = false;
        }
        public virtual void RunCommand(object sender)
        {
            System.Threading.Thread.Sleep(DefaultPause);
        }
        public virtual void RunCommand(object sender, Core.Script.ScriptAction command, System.ComponentModel.BackgroundWorker bgw)
        {
            System.Threading.Thread.Sleep(DefaultPause);
        }

        public virtual string GetDisplayValue()
        {
            return SelectionName;
        }
    }

Inherited and Implemented Automation Command:

  [Serializable]
    [Attributes.ClassAttributes.Group("Misc Commands")]
    [Attributes.ClassAttributes.Description("This command pauses the script for a set amount of time in milliseconds.")]
    [Attributes.ClassAttributes.ImplementationDescription("This command implements 'Thread.Sleep' to achieve automation.")]
    public class PauseCommand : ScriptCommand
    {
        [XmlAttribute]
        [Attributes.PropertyAttributes.PropertyDescription("Amount of time to pause for (in milliseconds).")]
        public int v_PauseLength { get; set; }
        public PauseCommand()
        {
            this.CommandName = "PauseCommand";
            this.SelectionName = "Pause - Pause Script";
            this.CommandEnabled = true;
        }
        public override void RunCommand(object sender)
        {
            System.Threading.Thread.Sleep(v_PauseLength);
        }
        public override string GetDisplayValue()
        {
            return base.GetDisplayValue() + " [Wait for " + v_PauseLength + "ms]";
        }
    }

When the main form loads, the application will query the taskt.Core.AutomationCommands namespace for all commands that are enabled (this.CommandEnabled = true). Enabled commands will then be displayed and allowed to be used for configuration by users. Adding a command calls the frmCommandEditor form, which will attempt to locate any parameters (properties starting with 'v_') from the selected command class. Any parameters found on the class are automatically generated on the UI so the user may provide inputs which bind back to an instance of that class and ultimately get saved as an XML.

By default, properties generate a textbox that the user supplies input into:

        [XmlAttribute]
        [Attributes.PropertyAttributes.PropertyDescription("Please Select or Type a window Name")]
        public string v_WindowName { get; set; } // This would generate a Textbox and use the description for a label

Additionally, UI selection options can be specified on the property, which will generate a combobox:

        [XmlAttribute]
        [Attributes.PropertyAttributes.PropertyDescription("Please Select a Window State")]
        [Attributes.PropertyAttributes.PropertyUISelectionOption("Maximize")]
        [Attributes.PropertyAttributes.PropertyUISelectionOption("Minimize")]
        [Attributes.PropertyAttributes.PropertyUISelectionOption("Restore")]
        public string v_WindowState { get; set; }

Complex data types (like DataTable) are overridden and managed by the Command Builder form based on parameter name (as well as any other functions like adding event handlers to the generated components or changing the default properties of the components being generated). This is something that is slated to be refactored - the parameter generation process should be fully implemented and encapsulated within the class.

Once a user makes a series of selections and configurations and saves, the commands are generated into an XML which is deserialized, interpreted, and executed against the given parameters.

The underlying XML that generates looks like:

<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Commands>
    <ScriptAction>
      <ScriptCommand xsi:type="CommentCommand" CommandName="CommentCommand" IsCommented="false" SelectionName="Comment - Add Code Comment" DefaultPause="250" LineNumber="1" PauseBeforeExeucution="false" v_Comment="Start Notepad, Send Keys, then close the window" CommandEnabled="true" />
    </ScriptAction>
    <ScriptAction>
      <ScriptCommand xsi:type="StartProcessCommand" CommandName="StartProcessCommand" IsCommented="false" SelectionName="Process - Start Process" DefaultPause="250" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_ProgramName="notepad.exe" />
    </ScriptAction>
    <ScriptAction>
      <ScriptCommand xsi:type="SendKeysCommand" CommandName="SendKeysCommand" IsCommented="false" SelectionName="Input - Send Keystrokes" DefaultPause="250" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_WindowName="Untitled - Notepad" v_TextToSend="Hello World!" />
    </ScriptAction>
    <ScriptAction>
      <ScriptCommand xsi:type="CloseWindowCommand" CommandName="CloseWindowCommand" IsCommented="false" SelectionName="Window - Close Window" DefaultPause="250" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_WindowName="Untitled - Notepad" />
    </ScriptAction>
  </Commands>
  <Variables />
</Script>
Clone this wiki locally