This project is read-only.

BUG_overflowException with AppendAsync method

Apr 14, 2009 at 11:59 PM
Hi,

I am working with the version posted on a precedent discussion under Windows Vista:
http://cid-eaf0a921258b5980.skydrive.live.com/self.aspx/.Public/D3/DynamicDataDisplay.zip

In the following code, I simply added a chartPlotter (called "plotter") to a window and a button to start the simulation.
If you start debugging, a StackOverFlowException will appear ("An unhandled exception of type 'System.StackOverflowException' occurred in WindowsBase.dll") .

Please, try it!

Now, if you think that the overflow exception appears because of this line of code : "Thread.Sleep(1)", try this:
  • Replace "Thread.Sleep(1)" by "Thread.Sleep(100)"
  • Put a breakpoint in the debug mode at the following line "_source.AppendAsync(Dispatcher, NewTrade);"
  • Start the debug and you are going to see the stackOverflow beginning in the call Stack of VS 2008. But here is the strange thing. If you don't see the stackOverflow beginning, stop the debug mode and start it a second time. If you still don't see it, stop and start a third time. And so on, and you are going the see the bug before the fifth try!!!
Now, Replace "Thread.Sleep(100)" by "Thread.Sleep(1000)" and you will never see the bug again...

This kind of bug didn't exist in the first release of ddd.

Please, can anyone help me on this?

I am using ddd for financial application on tick data (each transaction). Since a lot of transaction could appear in a second I still can not use my application to trade into the market.

Thanks a lot,
Kamel



XAML code:

<Window x:Class="ddd_overflow.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="http://research.microsoft.com/DynamicDataDisplay/1.0"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <my:ChartPlotter Margin="12,24,12,41" Name="plotter" />
        <Button Height="19" Margin="93,0,88,11" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button>
    </Grid>
</Window>


C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;

namespace ddd_overflow
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private ObservableDataSource<Trade> _source;
        private Random _rand;

        public Window1()
        {
            InitializeComponent();
            _rand = new Random();
            EditPlotter();
        }

        void EditPlotter()
        {
            _source = new ObservableDataSource<Trade>();
            _source.SetXMapping(x => x.Counter);
            _source.SetYMapping(y => y.Price);
 
            plotter.AddLineGraph(_source,
                    new Pen(Brushes.Gold, 3),
                    new PenDescription("Sin(x + phase)"));
        }

        void AddNewTrade(Trade NewTrade)
        {
            plotter.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
               (System.Threading.ThreadStart)delegate()
               {
                   try
                   {
                      ///////// Put a breakpoint at the following line! //////////////

                       _source.AppendAsync(Dispatcher, NewTrade);

                     ////////////////////////////////////////////////////////////////////////////////

                   }
                   catch (Exception ex)
                   {
                       MessageBox.Show(ex.ToString());
                   }
               }
           );

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(BeginSimulation);
            thread.Start();
        }

        void BeginSimulation()
        {
            int counter = 0;
            for (int i = 0; i < 1000000; i++)
            {
                counter++;
                
                Trade trade = new Trade();
                trade.Counter = counter;
                trade.Price = _rand.NextDouble();

                AddNewTrade(trade);

                Thread.Sleep(1);
            }
        }

        public class Trade
        {
            public double Counter { get; set; }
            public double Price { get; set; }
        }
    }
}


Apr 15, 2009 at 2:40 PM
Hi,

Market opening this morning, the application frozed right away! Lol...

The StackOverflowException doesn't come from the fact there are too much data but when when the data are updated too fast.

I am stuck, any help would be greatly appreciated :)

Thanks,
Kamel
Apr 16, 2009 at 10:25 AM
Hi, Kamel!

I've reproduced bug you've described.

You can change your method AddNewTrade method, removing calling of Dispatcher.BeginInvoke(...), so that the complete body of this method will look like this:
void AddNewTrade(Trade NewTrade)

        {

            _source.AppendAsync(Dispatcher, NewTrade);

        }

This stops StackOverflowException from appearing.

Also I can advice you to set property IsBackground to true at thread you are creating. This is not connected to DynamicDataDisplay but this will allow application to close when you close main window (when thread you are creating is not background, it will exist after main thread exiting and application will live forever even when you've closed main window).

Yours,

Mikhail Brinchuk,

DynamicDataDisplay Dev Team.

Apr 16, 2009 at 1:04 PM
As easy as deleting some code?
I love it!

Thanks, it works great!  :)

Kamel