Skip to content

Commit

Permalink
TPL Dataflow example app
Browse files Browse the repository at this point in the history
  • Loading branch information
cheungt6 committed Jun 27, 2020
1 parent f7757a5 commit f018515
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Windows.Input;

namespace ReflectionEmitClassGeneration.Command
namespace Common.Wpf.Command
{
public abstract class CommandBase : ICommand
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace ReflectionEmitClassGeneration.Command
namespace Common.Wpf.Command
{
public class GenericCommand : CommandBase
{
Expand Down
8 changes: 8 additions & 0 deletions Common.Wpf/Common.Wpf.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace ReflectionEmitClassGeneration.ViewModel
namespace Common.Wpf.ViewModel
{
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ Attempt at using docker-compose to spin up Zixia's Simple Mail Forwarder with ad
### React Native Firebase Social Auth
React Native samples on how to sign up users using Firebase's social auth and email/password signup
### Reflection.Emit Class Generation
C# .NET Core code and sample WPF app showing how to generate a new type at runtime using Reflection.Emit
C# .NET Core code and sample WPF app showing how to generate a new type at runtime using Reflection.Emit
### TPL Dataflow Processing Pipeline
C# .NET Core app to demo functionality of the TPL Dataflow library for parallel data processing
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Common.Wpf\Common.Wpf.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReflectionEmitClassGeneration", "ReflectionEmitClassGeneration.csproj", "{18A83D6A-D546-46A7-9D11-93B003A4F020}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReflectionEmitClassGeneration", "ReflectionEmitClassGeneration.csproj", "{18A83D6A-D546-46A7-9D11-93B003A4F020}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Wpf", "..\Common.Wpf\Common.Wpf.csproj", "{28D09CD8-3BA5-45D6-A81A-09FC5CF37E8D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{18A83D6A-D546-46A7-9D11-93B003A4F020}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18A83D6A-D546-46A7-9D11-93B003A4F020}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18A83D6A-D546-46A7-9D11-93B003A4F020}.Release|Any CPU.Build.0 = Release|Any CPU
{28D09CD8-3BA5-45D6-A81A-09FC5CF37E8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28D09CD8-3BA5-45D6-A81A-09FC5CF37E8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28D09CD8-3BA5-45D6-A81A-09FC5CF37E8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28D09CD8-3BA5-45D6-A81A-09FC5CF37E8D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;
using Common.Wpf.ViewModel;

namespace ReflectionEmitClassGeneration.ViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using ReflectionEmitClassGeneration.Command;
using ReflectionEmitClassGeneration.Types;
using ReflectionEmitClassGeneration.Types;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Common.Wpf.Command;
using Common.Wpf.ViewModel;

namespace ReflectionEmitClassGeneration.ViewModel
{
Expand Down Expand Up @@ -71,7 +72,8 @@ public void GenerateItems()

private async void StartDataFeed()
{
_feedRunning = true;
lock (_feedLock)
_feedRunning = true;
while (_feedRunning)
{
await Task.Delay(500);
Expand Down
9 changes: 9 additions & 0 deletions TPLDataflowProcessing/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="TPLDataflowProcessing.App"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TPLDataflowProcessing"
StartupUri="View/MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions TPLDataflowProcessing/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace TPLDataflowProcessing
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
64 changes: 64 additions & 0 deletions TPLDataflowProcessing/Model/MockPriceFeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Threading.Tasks;

namespace TPLDataflowProcessing.Model
{
public class MockPriceFeed
{
public class PriceEventArgs : EventArgs
{
public PriceEventArgs(string name, decimal price)
{
TimeStampUtc = DateTime.UtcNow;
Price = price;
Name = string.Intern(name);
}
public DateTime TimeStampUtc { get; }
public decimal Price { get; }
public string Name { get; }
}

public delegate void PriceEvent(MockPriceFeed sender, PriceEventArgs e);
public event PriceEvent OnNextPricePublished;


private decimal _lastPrice;
private Random _rand = new Random();
private object _feedLock = new object();
private bool _isRunning = false;

public MockPriceFeed(string feedName)
{
FeedName = feedName;
_lastPrice = _rand.Next(0, 100);
}

public string FeedName { get; }

public async void StartAsync()
{
lock (_feedLock)
{
if (_isRunning) return;
_isRunning = true;
}

while (_isRunning)
{
_lastPrice = Math.Abs(_lastPrice + (_rand.Next(-5, 5) / 10m));
var priceObj = new PriceEventArgs(FeedName, _lastPrice);
OnNextPricePublished(this, priceObj);
await Task.Delay(100);
}
}

public void Stop()
{
lock (_feedLock)
{
if (!_isRunning) return;
_isRunning = false;
}
}
}
}
21 changes: 21 additions & 0 deletions TPLDataflowProcessing/TPLDataflowProcessing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.11.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Processors\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common.Wpf\Common.Wpf.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions TPLDataflowProcessing/TPLDataflowProcessing.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TPLDataflowProcessing", "TPLDataflowProcessing.csproj", "{6F1C1BF7-46D9-4401-8C7A-E0D0CFE420C5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Wpf", "..\Common.Wpf\Common.Wpf.csproj", "{DAD4476B-351A-46EE-9D14-EF3902F18E75}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6F1C1BF7-46D9-4401-8C7A-E0D0CFE420C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F1C1BF7-46D9-4401-8C7A-E0D0CFE420C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F1C1BF7-46D9-4401-8C7A-E0D0CFE420C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F1C1BF7-46D9-4401-8C7A-E0D0CFE420C5}.Release|Any CPU.Build.0 = Release|Any CPU
{DAD4476B-351A-46EE-9D14-EF3902F18E75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAD4476B-351A-46EE-9D14-EF3902F18E75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAD4476B-351A-46EE-9D14-EF3902F18E75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAD4476B-351A-46EE-9D14-EF3902F18E75}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7625D392-52B8-4A05-86AF-68EC73AC5669}
EndGlobalSection
EndGlobal
64 changes: 64 additions & 0 deletions TPLDataflowProcessing/View/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<Window x:Class="TPLDataflowProcessing.MainWindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewmodel="clr-namespace:TPLDataflowProcessing.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="900">
<Window.DataContext>
<viewmodel:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="*"/>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Content="Feed"/>
<Label Grid.Row="0"
Grid.Column="1"
Content="10 Update Intervals"/>
<Label Grid.Row="0"
Grid.Column="2"
Content="1 Second Intervals"/>
<ListView Grid.Row="1"
Grid.Column="0"
ItemsSource="{Binding UpdateLog}" />
<DataGrid Grid.Row="1"
Grid.Column="1"
ItemsSource="{Binding AveragePrices}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="TimeStamp" Binding="{Binding TimeStampUtc}"/>
<DataGridTextColumn Header="Low" Binding="{Binding Low}"/>
<DataGridTextColumn Header="High" Binding="{Binding High}"/>
<DataGridTextColumn Header="Avg" Binding="{Binding Price}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1"
Grid.Column="2"
ItemsSource="{Binding LatestPrices}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" SortDirection="Ascending" SortMemberPath="Name"/>
<DataGridTextColumn Header="TimeStamp" Binding="{Binding TimeStampUtc}"/>
<DataGridTextColumn Header="Low" Binding="{Binding Low}"/>
<DataGridTextColumn Header="High" Binding="{Binding High}"/>
<DataGridTextColumn Header="Latest" Binding="{Binding Price}"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="2"
Grid.Column="1"
Command="{Binding StartFeedsCommand}">Start Feeds</Button>
<Button Grid.Row="2"
Grid.Column="2"
Command="{Binding StopFeedsCommand}">Stop Feeds</Button>
</Grid>
</Window>
28 changes: 28 additions & 0 deletions TPLDataflowProcessing/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TPLDataflowProcessing
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit f018515

Please sign in to comment.