Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

wpf graph error: Cannot draw outside of a draw cycle

Solved!
Go to solution

I am getting this error in my wpf application with Measurement Studio 2013. After much head scratching, I realised this error only occurs if I add plot to the graph from this.Dispatcher.Invoke() method from my background worker but if I moved adding the plots to my main thread (RunWorkerCompleted code) I don't get this error again. I know this error has been reported here before and this was attributed to not having the latest Measurement Studio library but I am using the latest library. But I need to move this back to the background worker because adding the plot in the main thread hangs my UI. Thanks.

0 Kudos
Message 1 of 8
(6,504 Views)

Unfortunately, I could not reproduce the issue from your description. I have included the code I used below. If you could, please provide a program that reproduces the problem, or include the call stack from the exception you are seeing.


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

        <ni:Graph x:Name="graph" />
        <ni:Legend Grid.Column="1" ItemsSource="{Binding ElementName=graph}" />

        <Button Grid.Row="1" Content="_Add Plot" Click="OnAddPlotClicked" />
    </Grid>

    Code
    public partial class MainWindow : Window {
        private readonly ChartCollection<double> data;
        private readonly DispatcherTimer timer;

        public MainWindow( ) {
            InitializeComponent( );

            this.data = new ChartCollection<double>( );
            this.data.Append( 0.0 );
            this.data.Append( 1.0 );

            this.timer = new DispatcherTimer( TimeSpan.FromSeconds( 1 ), DispatcherPriority.Normal, this.OnTimerTick, this.Dispatcher );

            this.graph.DataSource = this.data;
        }

        private void OnTimerTick( object sender, EventArgs e ) {
            int index = this.data.Count - 1;
            double a = this.data[index - 1];
            double b = this.data[index];
            this.data.Append( a + b );
        }

        private void OnAddPlotClicked( object sender, RoutedEventArgs e ) {
            var worker = new BackgroundWorker( );
            worker.DoWork += delegate {
                Thread.Sleep( TimeSpan.FromSeconds( 1 ) );
#if DISPATCHER_INVOKE
                this.Dispatcher.Invoke( new Action( delegate {
                    graph.Plots.Add( CreatePlot( graph.Plots.Count ) );
                } ) );
#endif
            };
#if !DISPATCHER_INVOKE
            worker.RunWorkerCompleted += delegate {
                graph.Plots.Add( CreatePlot( graph.Plots.Count ) );
            };
#endif
            worker.RunWorkerAsync( );
        }

        private static Plot CreatePlot( int i ) {
            var renderer = new PointPlotRenderer { StrokeThickness = 0 };
            string label = "Plot #" + i;
            return new Plot( label ) { Renderer = renderer };
        }
    }

~ Paul H
0 Kudos
Message 2 of 8
(6,468 Views)

Hi Paul,

 

Thank you for your reply. Please find attached a project tha reproduces the issue. Thanks.

0 Kudos
Message 3 of 8
(6,382 Views)
Solution
Accepted by topic author falopsy

I was able to reproduce the error with your project. The issue is with the implementation of the Auto render mode in Measurement Studio 2013, where cached render args using the old target were getting invoked after the new target was selected.


To avoid this, set RenderMode to either Vector or Raster in the ulGraph.xaml file.

~ Paul H
0 Kudos
Message 4 of 8
(6,368 Views)

Aha! That solved the issue. Thank you very much. But the error message is very vague (and no inner exception). I think the current implementation of measurement studio should be much more user friendly so that debugging can be done by the end user. Thank you very much for the fast reply as usual.

 

I hope this bug (yes it is a bug, using Rendering="Auto" should not throw an exception) will be corrected in a new release of visual studio.

 

Thank you.

0 Kudos
Message 5 of 8
(6,358 Views)

Yes, I did create a task to ensure this is fixed (sorry I did not mention it before). I will add your feedback about improving debugability.

~ Paul H
0 Kudos
Message 6 of 8
(6,353 Views)

Thank you.

0 Kudos
Message 7 of 8
(6,350 Views)
Solution
Accepted by topic author falopsy

Just wanted to let you know this issue (#450981) was fixed in the Measurement Studio 2015 release.

~ Paul H
0 Kudos
Message 8 of 8
(4,886 Views)