Skip to content

Commit

Permalink
Sample: Tasks (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfevia authored Jul 13, 2024
1 parent c88dee2 commit 56c7342
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 1 deletion.
10 changes: 10 additions & 0 deletions PowerAutomate.Desktop.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AssemblyResolution", "Assem
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.AssemblyResolution.Actions", "samples\Modules.AssemblyResolution.Actions\Modules.AssemblyResolution.Actions.csproj", "{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.Tasks.Actions", "samples\Modules.Tasks.Actions\Modules.Tasks.Actions.csproj", "{801F4464-7843-4252-9476-E68830781126}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tasks", "Tasks", "{33E32C24-4A59-4DE1-B9C5-57CF051AD355}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +99,10 @@ Global
{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Release|Any CPU.Build.0 = Release|Any CPU
{801F4464-7843-4252-9476-E68830781126}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{801F4464-7843-4252-9476-E68830781126}.Debug|Any CPU.Build.0 = Debug|Any CPU
{801F4464-7843-4252-9476-E68830781126}.Release|Any CPU.ActiveCfg = Release|Any CPU
{801F4464-7843-4252-9476-E68830781126}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -118,6 +126,8 @@ Global
{1D4838E3-22A7-4D65-91CF-2395778EE92F} = {A5E3C936-7C1F-4719-9EAA-37F4AE439EF7}
{8EC670BB-BC22-44B4-8228-8F053188C908} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4}
{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD} = {8EC670BB-BC22-44B4-8228-8F053188C908}
{801F4464-7843-4252-9476-E68830781126} = {33E32C24-4A59-4DE1-B9C5-57CF051AD355}
{33E32C24-4A59-4DE1-B9C5-57CF051AD355} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Modules.Actions.Shared\Modules.Actions.Shared.projitems*{7e19e2f1-6de7-4a0e-aed8-7cea38b166af}*SharedItemsImports = 13
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyTitle>GettingStarted</AssemblyTitle>
<AssemblyTitle>AssemblyResolution</AssemblyTitle>
<TargetFramework>net472</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
Expand Down
10 changes: 10 additions & 0 deletions samples/Modules.Tasks.Actions/ErrorCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

namespace PowerAutomate.Desktop.Modules.Tasks.Actions;

internal static class ErrorCodes
{
public const string Unknown = "UnknownError";
}
16 changes: 16 additions & 0 deletions samples/Modules.Tasks.Actions/Modules.Tasks.Actions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyTitle>Tasks</AssemblyTitle>
<TargetFramework>net472</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<PackCustomModule Condition=" '$(Configuration)' == 'Debug' ">true</PackCustomModule>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions samples/Modules.Tasks.Actions/TaskDelayAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

using System;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes;

namespace PowerAutomate.Desktop.Modules.Tasks.Actions;

[Action(Id = "Delay")]
[Throws(ErrorCodes.Unknown)]
public class TaskDelayAction : ActionBase
{
[InputArgument(Order = 2)]
public int DelayInMilliseconds { get; set; }

[InputArgument(Order = 1)]
public string Name { get; set; } = null!;

[OutputArgument(Order = 0)]
public TaskObject Task { get; set; } = null!;

public override void Execute(ActionContext context)
{
try
{
var task = System.Threading.Tasks.Task.Delay(DelayInMilliseconds);
Task = new TaskObject(Name, task);
}
catch (Exception ex)
{
throw new ActionException(ErrorCodes.Unknown, ex.Message, ex);
}
}
}
49 changes: 49 additions & 0 deletions samples/Modules.Tasks.Actions/TaskObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Enums;
using Newtonsoft.Json;

namespace PowerAutomate.Desktop.Modules.Tasks.Actions;

[JsonObject(MemberSerialization.OptOut)]
[Type(DefaultPropertyVisibility = Visibility.Visible)]
public class TaskObject : IComparable<TaskObject>, IComparable
{
public string Name { get; private set; } = null!;
internal Task Task { get; private set; } = null!;

[JsonConstructor]
public TaskObject()
{
}

internal TaskObject(string name, Task task)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Task = task ?? throw new ArgumentNullException(nameof(task));
}

public int CompareTo(object? obj)
{
if (ReferenceEquals(null, obj)) return 1;
if (ReferenceEquals(this, obj)) return 0;
return obj is TaskObject other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TaskObject)}");
}

public int CompareTo(TaskObject? other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
return string.Compare(Name, other.Name, StringComparison.InvariantCultureIgnoreCase);
}

public override string ToString()
{
return Name;
}
}
33 changes: 33 additions & 0 deletions samples/Modules.Tasks.Actions/TasksWhenAllAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes;

namespace PowerAutomate.Desktop.Modules.Tasks.Actions;

[Action(Id = "WhenAll")]
[Throws(ErrorCodes.Unknown)]
public class TasksWhenAllAction : ActionBase
{
[InputArgument]
public List<TaskObject> Tasks { get; set; } = null!;

public override void Execute(ActionContext context)
{
try
{
var tasks = Tasks.Select(task => task.Task).ToList();
Task.WhenAll(tasks).GetAwaiter().GetResult();
}
catch (Exception ex)
{
throw new ActionException(ErrorCodes.Unknown, ex.Message, ex);
}
}
}
33 changes: 33 additions & 0 deletions samples/Modules.Tasks.Actions/TasksWhenAnyAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes;

namespace PowerAutomate.Desktop.Modules.Tasks.Actions;

[Action(Id = "WhenAny")]
[Throws(ErrorCodes.Unknown)]
public class TasksWhenAnyAction : ActionBase
{
[InputArgument]
public List<TaskObject> Tasks { get; set; } = null!;

public override void Execute(ActionContext context)
{
try
{
var tasks = Tasks.Select(task => task.Task).ToList();
Task.WhenAny(tasks).GetAwaiter().GetResult();
}
catch (Exception ex)
{
throw new ActionException(ErrorCodes.Unknown, ex.Message, ex);
}
}
}

0 comments on commit 56c7342

Please sign in to comment.