Added support for Silverlight and Mono (And more)

Also added:
* Join, Choice, and Pipe to SmartThreadPool.
* Local performance counters (for Mono, Silverlight, and WindowsCE)
This commit is contained in:
Ami Bar
2009-12-19 17:36:39 +02:00
parent d2c14da5ad
commit 760bc5d1a9
61 changed files with 2960 additions and 969 deletions
+7
View File
@@ -0,0 +1,7 @@
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="STPSLDemo.App"
>
<Application.Resources>
</Application.Resources>
</Application>
+66
View File
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace STPSLDemo
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
+41
View File
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace STPSLDemo
{
public class Groupbox : ContentControl
{
public Groupbox()
{
DefaultStyleKey = this.GetType();
}
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(Groupbox), null);
public object Header
{
get { return GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(Groupbox), null);
public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}
}
}
+32
View File
@@ -0,0 +1,32 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:STPSLDemo="clr-namespace:STPSLDemo"
>
<Style TargetType="STPSLDemo:Groupbox" >
<Setter Property="BorderBrush" Value="DarkGray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Padding" Value="6"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="STPSLDemo:Groupbox">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border BorderThickness="{TemplateBinding BorderThickness}" Grid.Row="1" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" />
<ContentPresenter Grid.Row="2" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"/>
<ContentControl Margin="6,0,0,0" Grid.Row="0" Grid.RowSpan="2">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Margin="2,0,2,0" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}"/>
</Border>
</ContentControl>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
+147
View File
@@ -0,0 +1,147 @@
<UserControl x:Class="STPSLDemo.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:STPSLDemo="clr-namespace:STPSLDemo"
Width="Auto" Height="Auto" Loaded="UserControl_Loaded">
<UserControl.Resources>
<Style x:Name="GroupboxStyle" TargetType="STPSLDemo:Groupbox">
<Setter Property="BorderBrush" Value="DarkGray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Padding" Value="6"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="STPSLDemo:Groupbox">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border BorderThickness="{TemplateBinding BorderThickness}" Grid.Row="1" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" />
<ContentPresenter Grid.Row="2" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"/>
<ContentControl Margin="6,0,0,0" Grid.Row="0" Grid.RowSpan="2">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Margin="2,0,2,0" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}"/>
</Border>
</ContentControl>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Margin="10,10,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Test Smart Thread Pool" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.ColumnSpan="5"/>
<!--<TextBlock Text="STP Usage" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>-->
<!--<TextBlock Text="STP History Usage" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="3"/>-->
<STPSLDemo:Groupbox Header="STP Usage" Style="{StaticResource GroupboxStyle}" Grid.Row="2" Grid.Column="1" >
<STPSLDemo:UsageControl x:Name="_usageControl" Value1="90" Value2="100" Width="36"/>
</STPSLDemo:Groupbox>
<STPSLDemo:Groupbox Header="STP History Usage" Style="{StaticResource GroupboxStyle}" Grid.Row="2" Grid.Column="3" >
<STPSLDemo:UsageHistoryControl x:Name="_historyUsageControl" Height="100" Width="400"/>
</STPSLDemo:Groupbox>
<StackPanel Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="3" Orientation="Horizontal" Margin="0,10,0,0">
<STPSLDemo:Groupbox Header="Work Items" Style="{StaticResource GroupboxStyle}">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Queued" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="Generated" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="Completed" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2" Grid.Column="0"/>
<TextBlock x:Name="_queuedWorkItems" Text="0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" />
<TextBlock x:Name="_generatedWorkItems" Text="0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>
<TextBlock x:Name="_completedWorkItems" Text="0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="2" Grid.Column="1"/>
</Grid>
</STPSLDemo:Groupbox>
<TextBlock Width="20"/>
<STPSLDemo:Groupbox Header="Threads" Style="{StaticResource GroupboxStyle}">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="In pool (Red)" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="Used (Green)" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
<TextBlock x:Name="_threadsInPool" Text="0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" />
<TextBlock x:Name="_threadsUsed" Text="0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>
</Grid>
</STPSLDemo:Groupbox>
</StackPanel>
<StackPanel Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="5" Orientation="Vertical" HorizontalAlignment="Left" Margin="0,10,0,0">
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<STPSLDemo:SpinButton x:Name="_spinMinThreads" ValueChanged="_spinMinThreads_ValueChanged" Maximum="25"/>
<TextBlock Text="Minimum Threads" VerticalAlignment="Center" Margin="20,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<STPSLDemo:SpinButton x:Name="_spinMaxThreads" ValueChanged="_spinMaxThreads_ValueChanged" Minimum="1" Maximum="25"/>
<TextBlock Text="Maximum Threads" VerticalAlignment="Center" Margin="20,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<STPSLDemo:SpinButton x:Name="_spinIdleTimeout" Minimum="1" Maximum="100"/>
<TextBlock Text="Idle Timeout (Seconds)" VerticalAlignment="Center" Margin="20,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<STPSLDemo:SpinButton x:Name="_spinInterval" ValueChanged="_spinInterval_ValueChanged" Minimum="0" Maximum="100000" Step="100"/>
<TextBlock Text="Interval between work item production (milliseconds)" VerticalAlignment="Center" Margin="20,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<STPSLDemo:SpinButton x:Name="_spinWorkItemTime" ValueChanged="_spinWorkItemTime_ValueChanged" Minimum="0" Maximum="100000" Step="100"/>
<TextBlock Text="Work item consuming time (milliseconds)" VerticalAlignment="Center" Margin="20,0,0,0"/>
</StackPanel>
</StackPanel>
<StackPanel Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="6" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,20,0,0">
<Button x:Name="btnStart" Content="Start" Width="50" Click="Start_Click"/>
<TextBlock Width="20"/>
<Button x:Name="btnStop" Content="Stop" Width="50" Click="Stop_Click" IsEnabled="False"/>
</StackPanel>
</Grid>
</UserControl>
+208
View File
@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using Amib.Threading;
using System.Threading;
namespace STPSLDemo
{
public partial class Page : UserControl
{
private System.Windows.Threading.DispatcherTimer _timer;
private SmartThreadPool _stp;
private bool running;
private int workItemsCompleted;
private int workItemsGenerated;
private Thread workItemsProducerThread;
private int _interval = 0;
private int _consumingTime = 0;
public Page()
{
InitializeComponent();
_timer = new DispatcherTimer();
_timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
_timer.Tick += _timer_Tick;
_timer.Start();
_spinMinThreads.Value = 0;
_spinMaxThreads.Value = 10;
_spinIdleTimeout.Value = 5;
_spinWorkItemTime.Value = 100;
_spinInterval.Value = 100;
}
void _timer_Tick(object sender, EventArgs e)
{
SmartThreadPool stp = _stp;
if (null == stp)
{
return;
}
int threadsInUse = (int)_stp.PerformanceCountersReader.InUseThreads;
int threadsInPool = (int)_stp.PerformanceCountersReader.ActiveThreads;
_threadsUsed.Text = threadsInUse.ToString();
_threadsInPool.Text = threadsInPool.ToString();
_queuedWorkItems.Text = _stp.PerformanceCountersReader.WorkItemsQueued.ToString();
_usageControl.Value1 = threadsInUse;
_usageControl.Value2 = threadsInPool;
_completedWorkItems.Text = _stp.PerformanceCountersReader.WorkItemsProcessed.ToString();
_generatedWorkItems.Text = workItemsGenerated.ToString();
_historyUsageControl.AddValues(threadsInUse, threadsInPool);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
UpdateControls(false);
}
private void Start_Click(object sender, RoutedEventArgs e)
{
btnStart.IsEnabled = false;
btnStop.IsEnabled = true;
UpdateControls(true);
workItemsCompleted = 0;
workItemsGenerated = 0;
STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.IdleTimeout = _spinIdleTimeout.Value * 1000;
stpStartInfo.MaxWorkerThreads = _spinMaxThreads.Value;
stpStartInfo.MinWorkerThreads = _spinMinThreads.Value;
stpStartInfo.EnableLocalPerformanceCounters = true;
_stp = new SmartThreadPool(stpStartInfo);
workItemsProducerThread = new Thread(WorkItemsProducer) {IsBackground = true};
workItemsProducerThread.Start();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
running = false;
workItemsProducerThread.Join();
_stp.Shutdown(true, 1000);
_stp.Dispose();
_stp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
UpdateControls(false);
}
private void UpdateControls(bool start)
{
running = start;
_spinIdleTimeout.IsEnabled = !start;
btnStart.IsEnabled = !start;
btnStop.IsEnabled = start;
if (start)
{
_timer.Start();
}
else
{
_timer.Stop();
}
_threadsUsed.Text = "0";
_threadsInPool.Text = "0";
_queuedWorkItems.Text = "0";
_usageControl.Maximum = _spinMaxThreads.Maximum;
_usageControl.Value1 = 0;
_usageControl.Value2 = 0;
_completedWorkItems.Text = "0";
_generatedWorkItems.Text = "0";
_historyUsageControl.Reset();
_historyUsageControl.Maximum = _spinMaxThreads.Maximum;
}
private void WorkItemsProducer()
{
WorkItemCallback workItemCallback = DoWork;
while (running)
{
IWorkItemsGroup workItemsGroup = _stp;
if (null == workItemsGroup)
{
return;
}
try
{
workItemsGroup.QueueWorkItem(workItemCallback);
}
catch (ObjectDisposedException e)
{
e.GetHashCode();
break;
}
workItemsGenerated++;
Thread.Sleep(_interval);
}
}
private object DoWork(object obj)
{
Thread.Sleep(_consumingTime);
Interlocked.Increment(ref workItemsCompleted);
return null;
}
private void _spinMinThreads_ValueChanged(int newValue)
{
if (newValue > _spinMaxThreads.Value)
{
_spinMaxThreads.Value = newValue;
if (null != _stp)
{
_stp.MaxThreads = newValue;
}
}
if (null != _stp)
{
_stp.MinThreads = newValue;
}
}
private void _spinMaxThreads_ValueChanged(int newValue)
{
if (newValue < _spinMinThreads.Value)
{
_spinMinThreads.Value = newValue;
_usageControl.Maximum = newValue;
_historyUsageControl.Maximum = newValue;
if (null != _stp)
{
_stp.MinThreads = newValue;
}
}
if (null != _stp)
{
_stp.MaxThreads = newValue;
}
}
private void _spinInterval_ValueChanged(int obj)
{
_interval = _spinInterval.Value;
}
private void _spinWorkItemTime_ValueChanged(int obj)
{
_consumingTime = _spinWorkItemTime.Value;
}
}
}
+6
View File
@@ -0,0 +1,6 @@
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Deployment.Parts>
</Deployment.Parts>
</Deployment>
+35
View File
@@ -0,0 +1,35 @@
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("STPSLDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("STPSLDemo")]
[assembly: AssemblyCopyright("Copyright © 2008")]
[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("44ee4f12-169f-4949-91f7-efc7100c8be5")]
// 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 Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 701 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 735 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 B

+140
View File
@@ -0,0 +1,140 @@
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{44EE4F12-169F-4949-91F7-EFC7100C8BE5}</ProjectGuid>
<ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>STPSLDemo</RootNamespace>
<AssemblyName>STPSLDemo</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<SilverlightApplication>true</SilverlightApplication>
<SupportedCultures>
</SupportedCultures>
<XapOutputs>true</XapOutputs>
<GenerateSilverlightManifest>true</GenerateSilverlightManifest>
<XapFilename>STPSLDemo.xap</XapFilename>
<SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate>
<SilverlightAppEntry>STPSLDemo.App</SilverlightAppEntry>
<TestPageFileName>TestPage.html</TestPageFileName>
<CreateTestPage>true</CreateTestPage>
<ValidateXaml>true</ValidateXaml>
<ThrowErrorsInValidation>false</ThrowErrorsInValidation>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>Bin\Debug</OutputPath>
<DefineConstants>TRACE;DEBUG;SILVERLIGHT=1</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<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;SILVERLIGHT</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Windows" />
<Reference Include="mscorlib" />
<Reference Include="system" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
<Reference Include="System.Xml" />
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Page.xaml.cs">
<DependentUpon>Page.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpinButton.xaml.cs">
<DependentUpon>SpinButton.xaml</DependentUpon>
</Compile>
<Compile Include="UsageControl.xaml.cs">
<DependentUpon>UsageControl.xaml</DependentUpon>
</Compile>
<Compile Include="UsageHistoryControl.xaml.cs">
<DependentUpon>UsageHistoryControl.xaml</DependentUpon>
</Compile>
<Compile Include="Groupbox.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:MarkupCompilePass1</Generator>
</ApplicationDefinition>
<Page Include="Groupbox.xaml">
<Generator>MSBuild:MarkupCompilePass1</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Page.xaml">
<Generator>MSBuild:MarkupCompilePass1</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="SpinButton.xaml">
<SubType>Page</SubType>
<Generator>MSBuild:MarkupCompilePass1</Generator>
</Page>
<Page Include="UsageControl.xaml">
<SubType>Page</SubType>
<Generator>MSBuild:MarkupCompilePass1</Generator>
</Page>
<Page Include="UsageHistoryControl.xaml">
<SubType>Page</SubType>
<Generator>MSBuild:MarkupCompilePass1</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<None Include="Properties\AppManifest.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartThreadPool\SmartThreadPoolSL.csproj">
<Project>{A6590A96-22B3-4889-B80E-2C398810E441}</Project>
<Name>SmartThreadPoolSL</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\RedCell.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\GreenCell.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\EmptyCell.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.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>
-->
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{A1591282-1198-4647-A2B1-27E5FF5F6F3B}">
<SilverlightProjectProperties />
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>
+24
View File
@@ -0,0 +1,24 @@
<UserControl x:Class="STPSLDemo.SpinButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="Auto" Height="Auto" Loaded="UserControl_Loaded">
<StackPanel Orientation="Horizontal" Width="Auto" Grid.ColumnSpan="4">
<!-- Displays the current value. -->
<TextBox Name="lblCurrentValue" Background="White"
Height="30" Width = "50" VerticalContentAlignment="Center"
HorizontalContentAlignment="Center" FontSize="14" />
<StackPanel Orientation="Vertical">
<!-- The Up button. -->
<RepeatButton Height = "15" Width = "15" Name = "repeatAddValueButton" Delay = "200" Interval = "1"
Content = "+" FontSize="6" Click="Increment_Click" />
<!-- The Down button. -->
<RepeatButton Height = "15" Width = "15"
Name = "repeatRemoveValueButton" Delay = "200" Interval = "1"
Content = "-" FontSize="6" Click="Decrement_Click" />
</StackPanel>
</StackPanel>
</UserControl>
+108
View File
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace STPSLDemo
{
public partial class SpinButton : UserControl
{
public SpinButton()
{
InitializeComponent();
Step = 1;
Value = 5;
Maximum = 10;
Minimum = 0;
}
private void Increment_Click(object sender, RoutedEventArgs e)
{
int newValue = Math.Min(Value + Step, Maximum);
if (Value != newValue)
{
Value = newValue;
}
}
private void Decrement_Click(object sender, RoutedEventArgs e)
{
int newValue = Math.Max(Value - Step, Minimum);
if (Value != newValue)
{
Value = newValue;
}
}
private void UpdateLabel()
{
lblCurrentValue.Text = Value.ToString();
ValueChanged(Value);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
UpdateLabel();
}
public event Action<int> ValueChanged = newValue => { };
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(SpinButton), null);
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(int), typeof(SpinButton), null);
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(int), typeof(SpinButton), null);
public static readonly DependencyProperty StepProperty =
DependencyProperty.Register("Step", typeof(int), typeof(SpinButton), null);
public int Value
{
get { return (int)GetValue(ValueProperty); ; }
set
{
SetValue(ValueProperty, value);
UpdateLabel();
}
}
public int Step
{
get { return (int)GetValue(StepProperty); ; }
set
{
SetValue(StepProperty, value);
}
}
public int Minimum
{
get { return (int)GetValue(MinimumProperty); }
set
{
SetValue(MinimumProperty, value);
}
}
public int Maximum
{
get { return (int)GetValue(MaximumProperty); }
set
{
SetValue(MaximumProperty, value);
}
}
}
}
+28
View File
@@ -0,0 +1,28 @@
<UserControl x:Class="STPSLDemo.UsageControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="Auto" Width="Auto">
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" Background="Black" Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="18"/>
<ColumnDefinition Width="18"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="4"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.Column="0" Width="Auto" Height="Auto">
<Border.Background>
<ImageBrush ImageSource="Resources/RedCell.jpg"/>
</Border.Background>
</Border>
<Grid.Resources>
<ImageBrush x:Name="EmptyCell" ImageSource="Resources/EmptyCell.jpg"/>
<ImageBrush x:Name="GreenCell" ImageSource="Resources/GreenCell.jpg"/>
<ImageBrush x:Name="RedCell" ImageSource="Resources/RedCell.jpg"/>
</Grid.Resources>
</Grid>
</UserControl>
+109
View File
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace STPSLDemo
{
public partial class UsageControl : UserControl
{
private int _rows = 25;
private int _columns = 2;
private int _max = 100; // Maximum value for progress range
private int _value1 = 30; // Current progress
private int _value2 = 60; // Current progress
public int Value1
{
get { return _value1; }
set
{
_value1 = value;
UpdateDisplay();
}
}
public int Value2
{
get { return _value2; }
set
{
_value2 = value;
UpdateDisplay();
}
}
public int Maximum
{
get { return _max; }
set
{
_max = value;
UpdateDisplay();
}
}
public UsageControl()
{
InitializeComponent();
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
LayoutRoot.Height = _rows * 4;
LayoutRoot.Width = _columns * 18;
LayoutRoot.RowDefinitions.Clear();
for (int i = 0; i < _rows; i++)
{
LayoutRoot.RowDefinitions.Add(new RowDefinition());
}
UpdateDisplay();
}
private void UpdateDisplay()
{
LayoutRoot.Children.Clear();
for (int j = 0; j < _columns; j++)
{
for (int i = 0; i < _rows; i++)
{
Brush brush = EmptyCell;
int percent = i * _max / _rows;
if (percent <= _value2)
{
brush = RedCell;
}
if (percent <= _value1)
{
brush = GreenCell;
}
Border border = new Border { Background = brush };
Grid.SetRow(border, _rows - i - 1);
Grid.SetColumn(border, j);
LayoutRoot.Children.Add(border);
}
}
}
}
}
+8
View File
@@ -0,0 +1,8 @@
<UserControl x:Class="STPSLDemo.UsageHistoryControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Black" Loaded="LayoutRoot_Loaded">
<InkPresenter x:Name="_surface" Background="Black"/>
</Grid>
</UserControl>
+209
View File
@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace STPSLDemo
{
public partial class UsageHistoryControl : UserControl
{
private const int _squareWidth = 12;
private const int maxLastValuesCount = 2000;
private const int _shiftStep = 3;
private int _shift = 0;
private int[] lastValues1 = new int[maxLastValuesCount];
private int[] lastValues2 = new int[maxLastValuesCount];
private int nextValueIndex;
private int lastValuesCount;
private int max = 100;
private int min = 0;
public UsageHistoryControl()
{
InitializeComponent();
}
public void Reset()
{
lastValues1.Initialize();
lastValues2.Initialize();
nextValueIndex = 0;
lastValuesCount = 0;
Redraw();
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
Redraw();
}
private void Redraw()
{
_surface.Strokes.Clear();
for (int i = 0; i <= Width + _squareWidth; i += _squareWidth)
{
int x = i - _shift;
if (x>= 0 && x < Width)
{
DrawLine(Colors.Green, x, 0, x, (int) Height);
}
}
for (int i = 0; i < Height; i += _squareWidth)
{
DrawLine(Colors.Green, 0, i, (int)Width, i);
}
int startValueIndex = (nextValueIndex - 1 + maxLastValuesCount) % maxLastValuesCount;
int prevVal1 = GetRelativeValue(lastValues1[startValueIndex]);
int prevVal2 = GetRelativeValue(lastValues2[startValueIndex]);
for (int i = 1; i < lastValuesCount; ++i)
{
int index = nextValueIndex - 1 - i;
if (index < 0)
{
index += maxLastValuesCount;
}
int val1 = GetRelativeValue(lastValues1[index]);
int val2 = GetRelativeValue(lastValues2[index]);
//Brush redBrush =
// new LinearGradientBrush(
// new Point(0, -(int)Height / 2),
// new Point(0, (int)Height),
// Color.Black,
// Color.Red);
DrawLine(
Colors.Red,
(int)Width - (i - 1) * _shiftStep, (int)Height - prevVal2,
(int)Width - i * _shiftStep, (int)Height - val2);
//g.FillPolygon(
// redBrush,
// new Point[]
// {
// new Point((int)Width-(i-1)*_shiftStep, (int)Height-prevVal2),
// new Point((int)Width-i*_shiftStep, (int)Height-val2),
// new Point((int)Width-i*_shiftStep, (int)Height),
// new Point((int)Width-(i-1)*_shiftStep, (int)Height),
// });
//Brush greenBrush =
// new LinearGradientBrush(
// new Point(0, -(int)Height / 2),
// new Point(0, (int)Height),
// Color.Black,
// Color.LawnGreen);
DrawLine(
Color.FromArgb(255, 124, 252, 0), // Colors.LawnGreen
(int)Width - (i - 1) * _shiftStep, (int)Height - prevVal1,
(int)Width - i * _shiftStep, (int)Height - val1);
//g.FillPolygon(
// greenBrush,
// new Point[]
// {
// new Point((int)Width-(i-1)*_shiftStep, (int)Height-prevVal1),
// new Point((int)Width-i*_shiftStep, (int)Height-val1),
// new Point((int)Width-i*_shiftStep, (int)Height),
// new Point((int)Width-(i-1)*_shiftStep, (int)Height),
// });
prevVal1 = val1;
prevVal2 = val2;
}
}
private int GetRelativeValue(int val)
{
int result = val * ((int)Height - 2) / max + 1;
return result;
}
public void AddValues(int val1, int val2)
{
lastValues1[nextValueIndex] = val1;
lastValues2[nextValueIndex] = val2;
nextValueIndex++;
nextValueIndex %= maxLastValuesCount;
lastValuesCount++;
if (lastValuesCount > maxLastValuesCount)
{
lastValuesCount = maxLastValuesCount;
}
_shift += _shiftStep;
_shift %= _squareWidth;
Redraw();
}
public int Maximum
{
get
{
return max;
}
set
{
// Make sure that the maximum value is never set lower than the minimum value.
if (value < min)
{
min = value;
}
max = value;
// Invalidate the control to get a repaint.
Redraw();
}
}
private void DrawLine(Color color, int x1, int y1, int x2, int y2)
{
if (x1 < 0 || x2 < 0)
{
return;
}
DrawPolygon(color, new Point(x1, y1), new Point(x2, y2));
}
private void DrawPolygon(
Color color,
params Point[] points)
{
StylusPointCollection stylusPointCollection = new StylusPointCollection();
foreach (Point point in points)
{
stylusPointCollection.Add(new StylusPoint(point.X, point.Y));
}
Stroke stroke = new Stroke(stylusPointCollection);
stroke.DrawingAttributes.Color = color;
stroke.DrawingAttributes.Width = 1;
stroke.DrawingAttributes.Height = 1;
_surface.Strokes.Add(stroke);
}
}
}