Skip to content

Commit 6721160

Browse files
committed
Add AsyncAwaitMacro sample
Shows how the ExcelAsyncUtil.QueueAsMacro mechanism can be used to implement async macros with the C# async/await mechanism.
1 parent e2c0681 commit 6721160

File tree

7 files changed

+235
-0
lines changed

7 files changed

+235
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<DnaLibrary Name="AsyncAwaitMacro Add-In" RuntimeVersion="v4.0">
2+
<ExternalLibrary Path="AsyncAwaitMacro.dll" LoadFromBytes="true" Pack="true" />
3+
4+
<!--
5+
The RuntimeVersion attribute above allows two settings:
6+
* RuntimeVersion="v2.0" - for .NET 2.0, 3.0 and 3.5
7+
* RuntimeVersion="v4.0" - for .NET 4 and 4.5
8+
9+
Additional referenced assemblies can be specified by adding 'Reference' tags.
10+
These libraries will not be examined and registered with Excel as add-in libraries,
11+
but will be packed into the -packed.xll file and loaded at runtime as needed.
12+
For example:
13+
14+
<Reference Path="Another.Library.dll" Pack="true" />
15+
16+
Excel-DNA also allows the xml for ribbon UI extensions to be specified in the .dna file.
17+
See the main Excel-DNA site at http://excel-dna.net for downloads of the full distribution.
18+
-->
19+
20+
</DnaLibrary>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{FB69CBC0-0DAD-4AE9-A4C0-ADEBE82D8F2C}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AsyncAwaitMacro</RootNamespace>
11+
<AssemblyName>AsyncAwaitMacro</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="ExcelDna.Integration">
34+
<HintPath>packages\Excel-DNA.Lib.0.32.0\lib\ExcelDna.Integration.dll</HintPath>
35+
<Private>False</Private>
36+
</Reference>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="AsyncMacros.cs" />
47+
<Compile Include="ExcelAsyncTask.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Content Include="AsyncAwaitMacro-AddIn.dna">
52+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
53+
</Content>
54+
<None Include="packages.config" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
<PropertyGroup>
58+
<PostBuildEvent>xcopy "$(ProjectDir)packages\Excel-DNA.0.32.0\tools\ExcelDna.xll" "$(TargetDir)AsyncAwaitMacro-AddIn.xll*" /C /Y
59+
xcopy "$(TargetDir)AsyncAwaitMacro-AddIn.dna*" "$(TargetDir)AsyncAwaitMacro-AddIn64.dna*" /C /Y
60+
xcopy "$(ProjectDir)packages\Excel-DNA.0.32.0\tools\ExcelDna64.xll" "$(TargetDir)AsyncAwaitMacro-AddIn64.xll*" /C /Y
61+
"$(ProjectDir)packages\Excel-DNA.0.32.0\tools\ExcelDnaPack.exe" "$(TargetDir)AsyncAwaitMacro-AddIn.dna" /Y
62+
"$(ProjectDir)packages\Excel-DNA.0.32.0\tools\ExcelDnaPack.exe" "$(TargetDir)AsyncAwaitMacro-AddIn64.dna" /Y</PostBuildEvent>
63+
</PropertyGroup>
64+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
65+
Other similar extension points exist, see Microsoft.Common.targets.
66+
<Target Name="BeforeBuild">
67+
</Target>
68+
<Target Name="AfterBuild">
69+
</Target>
70+
-->
71+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.30602.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitMacro", "AsyncAwaitMacro.csproj", "{FB69CBC0-0DAD-4AE9-A4C0-ADEBE82D8F2C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{FB69CBC0-0DAD-4AE9-A4C0-ADEBE82D8F2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FB69CBC0-0DAD-4AE9-A4C0-ADEBE82D8F2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FB69CBC0-0DAD-4AE9-A4C0-ADEBE82D8F2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FB69CBC0-0DAD-4AE9-A4C0-ADEBE82D8F2C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

AsyncAwaitMacro/AsyncMacros.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using ExcelDna.Integration;
6+
7+
namespace AsyncAwaitMacro
8+
{
9+
public static class AsyncMacros
10+
{
11+
static dynamic Application = ExcelDnaUtil.Application;
12+
13+
[ExcelCommand(MenuName = "AsyncAwaitMacros", MenuText = "DumpDataSlowly")]
14+
public static void DumpDataSlowly()
15+
{
16+
ExcelAsyncTask.Run(DumpDataSlowlyImpl);
17+
}
18+
19+
static async Task DumpDataSlowlyImpl()
20+
{
21+
// All the code here will run on the main thread - (though any Tasks run internally may do work on separate threads)
22+
23+
Debug.Print("1> {0}", Thread.CurrentThread.ManagedThreadId);
24+
await Task.Delay(TimeSpan.FromSeconds(5));
25+
Application.Range["A1"].Value = DateTime.Now.ToString("> HH:mm:ss");
26+
27+
Debug.Print("2> {0}", Thread.CurrentThread.ManagedThreadId);
28+
await Task.Delay(TimeSpan.FromSeconds(5));
29+
Application.Range["A2"].Value = DateTime.Now.ToString("> HH:mm:ss");
30+
31+
Debug.Print("3> {0}", Thread.CurrentThread.ManagedThreadId);
32+
await Task.Delay(TimeSpan.FromSeconds(5));
33+
Application.Range["A3"].Value = DateTime.Now.ToString("> HH:mm:ss");
34+
35+
Debug.Print("4> {0}", Thread.CurrentThread.ManagedThreadId);
36+
}
37+
38+
}
39+
}

AsyncAwaitMacro/ExcelAsyncTask.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace ExcelDna.Integration
7+
{
8+
public static class ExcelAsyncTask
9+
{
10+
public static Task Run(Func<Task> function)
11+
{
12+
return Task<Task>.Factory.StartNew(function, CancellationToken.None, TaskCreationOptions.DenyChildAttach, ExcelAsyncTaskScheduler.Instance).Unwrap();
13+
}
14+
}
15+
16+
class ExcelAsyncTaskScheduler : TaskScheduler
17+
{
18+
protected override void QueueTask(Task task)
19+
{
20+
ExcelAsyncUtil.QueueAsMacro(PostCallback, task);
21+
}
22+
23+
void PostCallback(object obj)
24+
{
25+
Task task = (Task)obj;
26+
TryExecuteTask(task);
27+
}
28+
29+
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
30+
{
31+
// TODO: We might add a mechanism that tries this...
32+
return false;
33+
}
34+
35+
protected override IEnumerable<Task> GetScheduledTasks()
36+
{
37+
return null;
38+
}
39+
40+
internal static ExcelAsyncTaskScheduler Instance = new ExcelAsyncTaskScheduler();
41+
}
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("AsyncAwaitMacro")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("AsyncAwaitMacro")]
13+
[assembly: AssemblyCopyright("Copyright © 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("ccd8a0e6-db06-405e-bcdd-737a580449a9")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

AsyncAwaitMacro/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Excel-DNA" version="0.32.0" targetFramework="net45" />
4+
<package id="Excel-DNA.Lib" version="0.32.0" targetFramework="net45" />
5+
</packages>

0 commit comments

Comments
 (0)