Click here to Skip to main content
15,891,136 members
Articles / High Performance Computing / Parallel Processing

TLS: An exercise in concurrent programming

Rate me:
Please Sign up or sign in to vote.
4.48/5 (11 votes)
11 May 2008CPOL18 min read 39.9K   330   44  
A walkthough about multi-threading an app and a useful helper class
<Window x:Class="TLS.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TLS"
    Title="Thread Local Storage Demo"
    Name="Window"
    WindowStartupLocation="CenterScreen"
    Height="300" Width="450">
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="5" />
            </Style>
            <local:RunningConverter x:Key="RunningConverter" />
            <local:IntConverter x:Key="IntConverter" />
        </Grid.Resources>

        <Border Grid.Column="0" Grid.Row="0" Margin="10" BorderThickness="1" BorderBrush="Black" CornerRadius="10">
            <ListBox Name="_ListBox" BorderThickness="0" Margin="10" MinWidth="100" ItemsSource="{x:Static local:TestInfo.Tests}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <StackPanel.Resources>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Margin" Value="3" />
                                </Style>
                            </StackPanel.Resources>
                            <TextBlock Text="{Binding Attribute.Index}" />
                            <TextBlock Text="{Binding Attribute.Name}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Border>

        <Border Grid.Column="1" Grid.Row="0" Margin="10" BorderThickness="1" BorderBrush="Black" CornerRadius="10">
            <Grid Margin="10" DataContext="{Binding ElementName=_ListBox, Path=SelectedItem}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0" Text="Test:" />
                <TextBlock Grid.Column="0" Grid.Row="1" Text="Run:" VerticalAlignment="Center" />
                <TextBlock Grid.Column="0" Grid.Row="2" Text="Ops / sec / core :" />
                <TextBlock Grid.Column="0" Grid.Row="3" Text="Ops / sec   total :" />
                
                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Attribute.Name}" />
                <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding OperationsPerSecondPerCore, Converter={StaticResource IntConverter}}" />
                <TextBlock Grid.Column="1" Grid.Row="3" Text="{Binding OperationsPerSecond, Converter={StaticResource IntConverter}}" />
               
                <Button Grid.Column="1" Grid.Row="1" Name="btnStop" Width="80" Margin="0,10"
                        Content="{Binding ElementName=Window, Path=Running, Converter={StaticResource RunningConverter}}"
                        HorizontalAlignment="Left" VerticalAlignment="Center" Padding="10,3"/>
            
            </Grid>
        </Border>
        
        <ProgressBar Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="10" Name="_ProgressBar" Height="42" VerticalAlignment="Center" />

    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions