Permalink
Browse files

Unityで使える非同期ライブラリアップします

  • Loading branch information...
1 parent cbbd83e commit 44737236edc11c2f8b59eec15ab2becf86b647c5 ppc committed Nov 9, 2012
View
@@ -1,6 +1,13 @@
-# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
-bin
-obj
-
-# mstest test results
-TestResults
+*.pidb
+*.suo
+*.userprefs
+*.vsmdi
+*.testsettings
+*/bin
+*/obj
+*/publish
+$tf
+TestResults
+!*.sln
+!*.csproj
+!*/*.csproj
View
@@ -0,0 +1,56 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorTasks", "IteratorTasks\IteratorTasks.csproj", "{3AB99F84-7E6F-4B01-9F57-AE5722C56893}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleTaskRunner", "SampleTaskRunner\SampleTaskRunner.csproj", "{5BF3A7F6-634B-4B1E-B3B2-134ECF8B1B26}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicSample", "BasicSample\BasicSample.csproj", "{B2D257A2-953D-434E-8056-C32E5F2D7489}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestIteratorTasks", "TestIteratorTasks\TestIteratorTasks.csproj", "{A3FBC7EE-46C0-4A22-BD44-FDFCE425672D}"
+EndProject
+Global
+ GlobalSection(TeamFoundationVersionControl) = preSolution
+ SccNumberOfProjects = 5
+ SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
+ SccTeamFoundationServer = https://tfs.codeplex.com/tfs/tfs37
+ SccLocalPath0 = .
+ SccProjectUniqueName1 = BasicSample\\BasicSample.csproj
+ SccProjectName1 = BasicSample
+ SccLocalPath1 = BasicSample
+ SccProjectUniqueName2 = IteratorTasks\\IteratorTasks.csproj
+ SccProjectName2 = IteratorTasks
+ SccLocalPath2 = IteratorTasks
+ SccProjectUniqueName3 = SampleTaskRunner\\SampleTaskRunner.csproj
+ SccProjectName3 = SampleTaskRunner
+ SccLocalPath3 = SampleTaskRunner
+ SccProjectUniqueName4 = TestIteratorTasks\\TestIteratorTasks.csproj
+ SccProjectName4 = TestIteratorTasks
+ SccLocalPath4 = TestIteratorTasks
+ EndGlobalSection
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {3AB99F84-7E6F-4B01-9F57-AE5722C56893}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {3AB99F84-7E6F-4B01-9F57-AE5722C56893}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {3AB99F84-7E6F-4B01-9F57-AE5722C56893}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {3AB99F84-7E6F-4B01-9F57-AE5722C56893}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5BF3A7F6-634B-4B1E-B3B2-134ECF8B1B26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5BF3A7F6-634B-4B1E-B3B2-134ECF8B1B26}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5BF3A7F6-634B-4B1E-B3B2-134ECF8B1B26}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5BF3A7F6-634B-4B1E-B3B2-134ECF8B1B26}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B2D257A2-953D-434E-8056-C32E5F2D7489}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B2D257A2-953D-434E-8056-C32E5F2D7489}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B2D257A2-953D-434E-8056-C32E5F2D7489}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B2D257A2-953D-434E-8056-C32E5F2D7489}.Release|Any CPU.Build.0 = Release|Any CPU
+ {A3FBC7EE-46C0-4A22-BD44-FDFCE425672D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A3FBC7EE-46C0-4A22-BD44-FDFCE425672D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A3FBC7EE-46C0-4A22-BD44-FDFCE425672D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A3FBC7EE-46C0-4A22-BD44-FDFCE425672D}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
View
@@ -0,0 +1,10 @@
+""
+{
+"FILE_VERSION" = "9237"
+"ENLISTMENT_CHOICE" = "NEVER"
+"PROJECT_FILE_RELATIVE_PATH" = ""
+"NUMBER_OF_EXCLUDED_FILES" = "0"
+"ORIGINAL_PROJECT_FILE_PATH" = ""
+"NUMBER_OF_NESTED_PROJECTS" = "0"
+"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROJECT"
+}
View
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<configuration>
+ <startup>
+
+ <supportedRuntime version="v2.0.50727"/></startup>
+</configuration>
View
@@ -0,0 +1,74 @@
+using System;
+using Aiming.IteratorTasks;
+
+namespace Sample
+{
+ /// <summary>
+ /// 3つのタスクを同時に動かす例。
+ /// </summary>
+ class BasicSample
+ {
+ public static void Run()
+ {
+ var runner = new SampleTaskRunner.TaskRunner();
+
+ Common.ShowFrameTask(50).Start(runner);
+
+ new Task<string>(Worker1)
+ .OnComplete(t => Console.WriteLine("Worker 1 Done: " + t.Result))
+ .Start(runner);
+
+ new Task<int>(Worker2)
+ .OnComplete(t => Console.WriteLine("Worker 2 Done: " + t.Result))
+ .Start(runner);
+
+ runner.Update(200);
+ }
+
+ /// <summary>
+ /// 30フレーム掛けて何かやった体で、文字列を返すコルーチン。
+ ///
+ /// 1フレームに処理が集中しないように分割して実行するイメージ。
+ /// </summary>
+ /// <param name="completed"></param>
+ /// <returns></returns>
+ private static System.Collections.IEnumerator Worker1(Action<string> completed)
+ {
+ Console.WriteLine("Start Worker 1");
+
+ for (int i = 0; i < 30; i++)
+ {
+ yield return null;
+ }
+
+ completed("Result");
+ }
+
+ /// <summary>
+ /// 3秒掛けて何かやった体で、数値を返すコルーチン。
+ ///
+ /// スレッドを立ててスリープしている部分を、時間がかかる計算や、ネットワーク待ちに置き換えて考えていただけると。
+ /// </summary>
+ /// <param name="completed"></param>
+ /// <returns></returns>
+ private static System.Collections.IEnumerator Worker2(Action<int> completed)
+ {
+ bool done = false;
+ int result = 0;
+
+ System.Threading.ThreadPool.QueueUserWorkItem(state =>
+ {
+ System.Threading.Thread.Sleep(3000);
+ result = 999;
+ done = true;
+ });
+
+ Console.WriteLine("Start Worker 2");
+
+ while (!done)
+ yield return null;
+
+ completed(result);
+ }
+ }
+}
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{B2D257A2-953D-434E-8056-C32E5F2D7489}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>BasicSample</RootNamespace>
+ <AssemblyName>BasicSample</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <TargetFrameworkProfile />
+ <SccProjectName>SAK</SccProjectName>
+ <SccLocalPath>SAK</SccLocalPath>
+ <SccAuxPath>SAK</SccAuxPath>
+ <SccProvider>SAK</SccProvider>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="BasicSample.cs" />
+ <Compile Include="Common.cs" />
+ <Compile Include="ContinuationSample.cs" />
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="App.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\IteratorTasks\IteratorTasks.csproj">
+ <Project>{3ab99f84-7e6f-4b01-9f57-ae5722c56893}</Project>
+ <Name>IteratorTasks</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\SampleTaskRunner\SampleTaskRunner.csproj">
+ <Project>{5bf3a7f6-634b-4b1e-b3b2-134ecf8b1b26}</Project>
+ <Name>SampleTaskRunner</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
@@ -0,0 +1,27 @@
+using System;
+using Aiming.IteratorTasks;
+
+namespace Sample
+{
+ class Common
+ {
+ public static Task ShowFrameTask(int numFrames)
+ {
+ return new Task(() => ShowFrame(numFrames));
+ }
+
+ /// <summary>
+ /// 毎フレーム、500ミリ秒スリープして、フレーム数を表示する。
+ /// </summary>
+ /// <returns></returns>
+ public static System.Collections.IEnumerator ShowFrame(int numFrames)
+ {
+ for (int i = 0; i < numFrames; i++)
+ {
+ System.Threading.Thread.Sleep(500);
+ Console.WriteLine("frame: {0}", i);
+ yield return null;
+ }
+ }
+ }
+}
@@ -0,0 +1,107 @@
+using System;
+using Aiming.IteratorTasks;
+
+namespace Sample
+{
+ /// <summary>
+ /// 継続処理のサンプル。
+ ///
+ /// 同期処理とイテレーター非同期処理の対応関係を説明。
+ /// U F(T x) → IEnumerator FAsync(T x, Action&lt;U&lt;);
+ /// F2(F1(x)) → new Task&lt;U&lt;(c => F1Async(x, c).ContinueWith&lt;V&lt;(F2Async);
+ /// </summary>
+ class ContinuationSample
+ {
+ public static void Run()
+ {
+ var x = 1.41421356;
+
+ // 同期処理
+ var result = F3(F2(F1(x)));
+ Console.WriteLine("同期処理の結果: " + result);
+
+ // イテレーター非同期処理
+ var task = new Task<double>(c => F1Async(x, c))
+ .ContinueWith<string>(F2Async)
+ .ContinueWith<int>(F3Async)
+ .OnComplete(t => Console.WriteLine("非同期処理の結果: " + t.Result));
+
+ var runner = new SampleTaskRunner.TaskRunner();
+
+ task.Start(runner);
+ Common.ShowFrameTask(50).Start(runner);
+
+ runner.Update(20);
+ }
+
+ #region 同期処理
+
+ // 中身には深い意味なし。
+ // 単に、F3(F2(F1(x))) みたいに繋ぎたいだけ。
+
+ private static double F1(double x)
+ {
+ return x * x;
+ }
+
+ private static string F2(double x)
+ {
+ return x.ToString();
+ }
+
+ private static int F3(string s)
+ {
+ return s.Length;
+ }
+
+ #endregion
+ #region イテレーター非同期処理
+
+ /// <summary>
+ /// 5フレーム後にF1の結果を返す。
+ /// </summary>
+ private static System.Collections.IEnumerator F1Async(double x, Action<double> completed)
+ {
+ for (int i = 0; i < 5; i++)
+ {
+ yield return null;
+ }
+
+ Console.WriteLine("F1Async 終了");
+ var result = F1(x);
+ completed(result);
+ }
+
+ /// <summary>
+ /// 5フレーム後にF2の結果を返す。
+ /// </summary>
+ private static System.Collections.IEnumerator F2Async(double x, Action<string> completed)
+ {
+ for (int i = 0; i < 5; i++)
+ {
+ yield return null;
+ }
+
+ Console.WriteLine("F2Async 終了");
+ var result = F2(x);
+ completed(result);
+ }
+
+ /// <summary>
+ /// 5フレーム後にF3の結果を返す。
+ /// </summary>
+ private static System.Collections.IEnumerator F3Async(string s, Action<int> completed)
+ {
+ for (int i = 0; i < 5; i++)
+ {
+ yield return null;
+ }
+
+ Console.WriteLine("F3Async 終了");
+ var result = F3(s);
+ completed(result);
+ }
+
+ #endregion
+ }
+}
View
@@ -0,0 +1,12 @@
+
+namespace Sample
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ ContinuationSample.Run();
+ //BasicSample.Run();
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 4473723

Please sign in to comment.