Hi,
if I unterstand your question try following litte demo:
XAML MainWindow
<Window x:Class="WpfApp1.Window09"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp09"
mc:Ignorable="d"
Title="Demo BlockingCollection" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<Button Content="Start VisualWorld" Command="{Binding}" Width="200" Margin="5"/>
</StackPanel>
</Window>
ViewModel:
--- buggy forum software don't include this code, see attached txt file: 55058-x.txt
XAML VisualWorld:
<Window x:Class="WpfApp09.VisualWorld"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp09"
mc:Ignorable="d"
Title="VisualWorld" Height="450" Width="800">
<Grid>
<Canvas x:Name="cv"/>
</Grid>
</Window>
Codebehind VisualWorld:
using System.Collections.Concurrent;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApp09
{
/// <summary>
/// Interaction logic for VisualWorld.xaml
/// </summary>
public partial class VisualWorld : Window
{
public VisualWorld()
{
InitializeComponent();
}
private BlockingCollection<ColorAddress> graphQueue;
private Thread graphThread;
private Polyline pline = new Polyline();
private PointCollection pointCollection = new PointCollection();
public void RunGraphics(BlockingCollection<ColorAddress> graphQueue)
{
this.graphQueue = graphQueue;
// create initial values
pline.Stroke = new SolidColorBrush(Color.FromRgb(60, 125, 200));
pline.StrokeThickness = 1;
pline.Points = pointCollection;
for (int i = 0; i < 500; i++) pointCollection.Add(new Point(i * 4, 0));
cv.Children.Add(pline);
// start thread for painting
graphThread = new Thread(ColorAddressPainter);
graphThread.IsBackground = true;
graphThread.Start();
}
private void ColorAddressPainter()
{
foreach (ColorAddress colorAddress in graphQueue.GetConsumingEnumerable())
{
Application.Current.Dispatcher.Invoke(() =>
{
Point pt = pointCollection[colorAddress.ValueX];
pt.Y = colorAddress.ValueY;
pointCollection[colorAddress.ValueX] = pt;
});
}
}
}
}
Result: