Commits

Igor Fesenko committed 66ad6df

[*] initial version

Comments (0)

Files changed (9)

+syntax: glob
+#OS junk files
+[Tt]humbs.db
+*.DS_Store
+
+#Visual Studio files
+*.[Oo]bj
+*.exe
+*.pdb
+*.user
+*.aps
+*.pch
+*.vspscc
+*.vssscc
+*_i.c
+*_p.c
+*.ncb
+*.suo
+*.tlb
+*.tlh
+*.[Cc]ache
+*.ilk
+*.log
+*.lib
+*.sbr
+*.sdf
+*.orig
+ipch/
+obj/
+[Bb]in
+[Dd]ebug*/
+[Rr]elease*/
+
+#Tooling
+_ReSharper*/
+*.resharper
+[Tt]est[Rr]esult*
+
+#Subversion files
+.svn
+[Cc][Vv][Ss]
+
+#Misc

ConsoleTester/ConsoleTester.csproj

+<?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>{680F6422-402C-461E-82EC-AB7CF820A58B}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>ConsoleTester</RootNamespace>
+    <AssemblyName>ConsoleTester</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </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="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\FluentTask\FluentTasks.csproj">
+      <Project>{92701dfb-7bc4-441d-a883-b6f7b87cc0c7}</Project>
+      <Name>FluentTasks</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>

ConsoleTester/Program.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading;
+using FluentTasks;
+
+namespace ConsoleTester
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var testRunner = new TestRunner();
+            testRunner.RunSynchronouslyTest();
+        }
+    }
+
+    class TestRunner
+    {
+         public void RunSynchronouslyTest()
+         {
+             new FluentTask()
+                 .DoWork(
+                     () =>
+                         {
+                             Console.WriteLine("Do work");
+                             Thread.Sleep(1000);
+                             throw new Exception("Test exception");
+                         })
+                 .OnException(Console.WriteLine)
+                 .OnComplete(() => Console.WriteLine("Complete work"))
+                 .RunSynchronously();
+         }
+
+         public void RunAsyncTest()
+         {
+             new FluentTask()
+                 .DoWork(
+                     () =>
+                     {
+                         Console.WriteLine("Do work");
+                         Thread.Sleep(1000);
+                         throw new Exception("Test exception");
+                     })
+                 .OnException(Console.WriteLine)
+                 .OnComplete(() => Console.WriteLine("Complete work"))
+                 .RunAsync();
+         }
+    }
+}

ConsoleTester/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("ConsoleTester")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("ConsoleTester")]
+[assembly: AssemblyCopyright("Copyright ©  2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("d4007e1c-29ea-41ee-a94b-568ed92943e5")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 11
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentTasks", "FluentTasks\FluentTasks.csproj", "{92701DFB-7BC4-441D-A883-B6F7B87CC0C7}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTester", "ConsoleTester\ConsoleTester.csproj", "{680F6422-402C-461E-82EC-AB7CF820A58B}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{680F6422-402C-461E-82EC-AB7CF820A58B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{680F6422-402C-461E-82EC-AB7CF820A58B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{680F6422-402C-461E-82EC-AB7CF820A58B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{680F6422-402C-461E-82EC-AB7CF820A58B}.Release|Any CPU.Build.0 = Release|Any CPU
+		{92701DFB-7BC4-441D-A883-B6F7B87CC0C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{92701DFB-7BC4-441D-A883-B6F7B87CC0C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{92701DFB-7BC4-441D-A883-B6F7B87CC0C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{92701DFB-7BC4-441D-A883-B6F7B87CC0C7}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

FluentTasks/FluentTask.cs

+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows.Threading;
+
+namespace FluentTasks
+{
+    public class FluentTask
+    {
+        private Action doWorkAction;
+        private Action onCompleteAction;
+        private Action<Exception> onExceptionAction;
+        private Dispatcher activeDispatcher; 
+
+        public FluentTask DoWork(Action action)
+        {
+            if (action == null)
+            {
+                throw new ArgumentNullException();
+            }
+            doWorkAction = action;
+            return this;
+        }
+
+        public FluentTask OnComplete(Action action)
+        {
+            if (action == null)
+            {
+                throw new ArgumentNullException();
+            }
+            onCompleteAction = action;
+            return this;
+        }
+
+        public FluentTask OnException(Action<Exception> action)
+        {
+            if (action == null)
+            {
+                throw new ArgumentNullException();
+            }
+            onExceptionAction = action;
+            return this;
+        }
+
+        public FluentTask UseDispatcher(Dispatcher dispatcher)
+        {
+            if(dispatcher == null)
+            {
+                throw new ArgumentNullException();
+            }
+            activeDispatcher = dispatcher;
+            return this;
+        }
+
+        #region Running
+
+        public void RunSynchronously()
+        {
+            try
+            {
+                if (doWorkAction != null)
+                {
+                    doWorkAction();
+                }
+            }
+            catch (Exception exception)
+            {
+                if(onExceptionAction != null)
+                {
+                    onExceptionAction(exception);
+                }
+            }
+            finally
+            {
+                if(onCompleteAction != null)
+                {
+                    onCompleteAction();
+                }
+            }
+        }
+
+        public void RunAsync()
+        {
+            var scheduler = TaskScheduler.Default;
+            var token = new CancellationToken();
+
+            Task.Factory
+                .StartNew(
+                    () =>
+                        {
+                            try
+                            {
+                                if (doWorkAction != null)
+                                {
+                                    doWorkAction();
+                                }
+                            }
+                            catch (Exception exception)
+                            {
+                                if (onExceptionAction != null)
+                                {
+                                    onExceptionAction(exception);
+                                }
+                            }
+                        })
+                .ContinueWith(
+                    task =>
+                        {
+                            if (onCompleteAction != null)
+                            {
+                                onCompleteAction();
+                            }
+                        }, token, TaskContinuationOptions.None, scheduler);
+        }
+
+        #endregion
+    }
+}

FluentTasks/FluentTasks.csproj

+<?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>{92701DFB-7BC4-441D-A883-B6F7B87CC0C7}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>FluentTasks</RootNamespace>
+    <AssemblyName>FluentTasks</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <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' ">
+    <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="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+    <Reference Include="WindowsBase" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="FluentTask.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </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>

FluentTasks/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("FluentTasks")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("FluentTasks")]
+[assembly: AssemblyCopyright("Copyright ©  2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("9642a9d0-0ade-4ac4-9775-4603c0a5d0fc")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
+Fluent Task
+=============================
+
+Summary
+-------------
+
+FluentTask allows you to run operations in separate thread yet receive success, failure and progress update on the WPF UI thread so that you can have a responsive UI and carry out expensive operations in background. It's convenient to use than BackgroundWorker component. No need to decare events and preserve stuffs in private variables to access it from different event callbacks. You can return data from parallel thread to the success/fail callback safely in a strongly typed manner.