Skip to content

Commit

Permalink
work around to prevent threading issues
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-rodriguez committed Mar 30, 2017
1 parent db2e430 commit faef669
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion WpfView/Axis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public ICommand PreviewRangeChangedCommand
[TypeConverter(typeof(StringCollectionConverter))]
public IList<string> Labels
{
get { return (IList<string>) GetValue(LabelsProperty); }
get { return ThreadAccess.Resolve<IList<string>>(this, LabelsProperty); }
set { SetValue(LabelsProperty, value); }
}

Expand Down
2 changes: 1 addition & 1 deletion WpfView/Charts/Base/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public LegendLocation LegendLocation
/// </summary>
public SeriesCollection Series
{
get { return (SeriesCollection)GetValue(SeriesProperty); }
get { return ThreadAccess.Resolve<SeriesCollection>(this, SeriesProperty); }
set { SetValue(SeriesProperty, value); }
}

Expand Down
48 changes: 48 additions & 0 deletions WpfView/Components/ThreadAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//The MIT License(MIT)

//Copyright(c) 2016 Alberto Rodriguez

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

using System;
using System.Windows;

namespace LiveCharts.Wpf.Components
{

// This is a workaround to prevent a possible threading issue
// LiveChart was designed to be easy to use, the current design
// avoids the usage of DataTemplates, instead we use the same object (UIElement)
// since UI elements are running the UI thread, it is possible that
// when we try to modify a property in a UIElement, i.e. the labels of an axis,
// we can't, well we can but we need to use the UI dispatcher.

internal static class ThreadAccess
{
public static T Resolve<T>(DependencyObject dependencyObject,
DependencyProperty dependencyProperty)
{
if (dependencyObject.Dispatcher.CheckAccess())
return (T) dependencyObject.GetValue(dependencyProperty);

return (T) dependencyObject.Dispatcher.Invoke(
new Func<T>(() => (T) dependencyObject.GetValue(dependencyProperty)));
}
}
}
10 changes: 2 additions & 8 deletions WpfView/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,14 @@ public IEnumerable<ChartPoint> ChartPoints
public static readonly DependencyProperty ValuesProperty = DependencyProperty.Register(
"Values", typeof (IChartValues), typeof (Series),
new PropertyMetadata(default(IChartValues), OnValuesInstanceChanged));

/// <summary>
/// Gets or sets chart values.
/// </summary>
[TypeConverter(typeof(NumericChartValuesConverter))]
public IChartValues Values
{
get
{
if (Application.Current == null || Application.Current.Dispatcher.CheckAccess())
return (IChartValues) GetValue(ValuesProperty);

return (IChartValues) Application.Current.Dispatcher.Invoke(
new Func<IChartValues>(() => (IChartValues) GetValue(ValuesProperty)));
}
get { return ThreadAccess.Resolve<IChartValues>(this, ValuesProperty); }
set { SetValue(ValuesProperty, value); }
}

Expand Down
1 change: 1 addition & 0 deletions WpfView/WpfView.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="ColorsCollection.cs" />
<Compile Include="Components\DefaultXamlReader.cs" />
<Compile Include="Components\IFondeable.cs" />
<Compile Include="Components\ThreadAccess.cs" />
<Compile Include="IChartLegend.cs" />
<Compile Include="IChartTooltip.cs" />
<Compile Include="LogarithmicAxis.cs" />
Expand Down

0 comments on commit faef669

Please sign in to comment.