Thread & Repaint problems

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Thread & Repaint problems

Post by Christopher » Wed Jan 08, 2014 4:08 pm

Hello Dominik,

Okay, I'm using this code:

Code: Select all

  public partial class Form5 : Form
  {
    TChart tChart1;
    System.Windows.Forms.Panel panel1;
    public Timer timer;

    public Form5()
    {
      InitializeComponent();
      CreateChart();
      InitializeChart();

      timer = new Timer(this.TimerRun, null, 0, 300);
    }


    private void InitializeChart()
    {
      tChart1.AutoRepaint = false;
      tChart1.Series.Add(typeof(FastLine));
    }

    private void CreateChart()
    {
      panel1 = new System.Windows.Forms.Panel();
      this.Controls.Add(panel1);
      tChart1 = new TChart();
      tChart1.Dock = DockStyle.Fill;
      panel1.Dock = DockStyle.Fill;
      panel1.Controls.Add(tChart1);
    }

    private void TimerRun(object sender)
    {
      var rnd1 = new Random();
      double f1 = rnd1.Next(100000);

      this.tChart1.AutoRepaint = false;
      //this.tChart1[0].Clear();
      for (int i = 0; i < 200; i++)
      {
        double id = this.tChart1[0].Count + 1;
        this.tChart1[0].Add(id, Math.Sin(id / f1));
       // Thread.Sleep(2);
      }

      this.tChart1.AutoRepaint = true;
      this.tChart1.Invalidate();
    }
  }
I build this in release mode and run the *.exe, resizing the form frequently. You can see a video of what I did here. I could not see any flickering or shadowing using this code, nor did the chart become unresponsive.

Of course, if you really do not want the chart to be resized while the thread is running and adding in data, all you have to do is to make sure the chart container (TChart or Panel or whatever) has its DockStyle set to something other than 'Fill'.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Re: Thread & Repaint problems

Post by moelski.net » Wed Jan 15, 2014 10:46 am

Hi Christopher,

sorry for the delay in my response. I was ill for some days ..

Anyway. I did some new tests in our main application. And I recorded a video which shows the problem:
http://www.logview.info/Temp/ChartProblem.mp4

Please take a look at that video. You will see that the chart get repainted while data is added.
But I switched off the AutoRepaint before adding data !

My "Code" for ading the data is like this (it´s a little bit stripped):
* this.tChart1.AutoRepaint = false;
* Clear all Series
* Open MySQL Connection and get the data which should be plotted
* Read data items and add it to the Sries: ....Line.Add(mydate, mySqlDataReader.GetDouble(i));
* this.tChart1.AutoRepaint = true;
* Recalculate Axis positions
* this.tChart1.Invalidate();

And the repainting happens while adding data. As you could see the data is received from a database (MySQL). This is not as fast as adding some 1000 random generated files. So maybe there is a problem in adding data soemthing slower? That´s why I added a Thread.Sleep in my demo code.

Would it be Ok for you if I generate a demo application which comes with a MySQL Server and the MySQL DLL and some Demo data?
That way you would be able to reproduce the problem.

Dominik

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Thread & Repaint problems

Post by Christopher » Wed Jan 15, 2014 11:05 am

Dominik,
moelski.net wrote: sorry for the delay in my response. I was ill for some days ..
I'm sorry to hear that and I hope you are fully recovered now.
moelski.net wrote: Would it be Ok for you if I generate a demo application which comes with a MySQL Server and the MySQL DLL and some Demo data?
That way you would be able to reproduce the problem.
It would be useful to have some code to reproduce the problem, but using MySQL should not be necessary as you are using a Series.Add() overload to add in the data meaning the problem should be reproducible without a database.

Can you please try and reproduce the problem using the Series.Add() overload but without the database?
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Re: Thread & Repaint problems

Post by moelski.net » Wed Jan 15, 2014 3:18 pm

Hi Christopher,

this is really a strange thing I´m working on :roll: But I think I get a little bit clother...

In my application I click a button. This button creates the (MDI)window with a fresh chart. Then the series and axes will be added (according to the database columns).
And then I start the thread for getting the data immediately.

If I do so I get the result I showed you in the video.

Now comes the tricky part - and I hope you can give me an explanation for that :?:
If the window is created (and all axes / series are created) I execute

Code: Select all

Application.DoEvents();
Then I start the thread for adding / reading the data. And the problem is gone.

So it seems to me that the chart is busy with something after it was created. And I start the data add thread to quick and something goes wrong - which causes this rubbish view.

Can you imagine what the main problem is here?

Dominik

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Thread & Repaint problems

Post by Christopher » Wed Jan 15, 2014 3:30 pm

Dominik
moelski.net wrote: Can you imagine what the main problem is here?
If only my imagination was that big!

Can I confirm that Application.DoEvents() solves the problem for you? If it does, then maybe it is a question of understanding why this is needed. Doing a quick look around, I see there are two stackoverflow questions which might help you understand what is going on, one here and the other here.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Re: Thread & Repaint problems

Post by moelski.net » Wed Jan 15, 2014 3:50 pm

Hi !
Can I confirm that Application.DoEvents() solves the problem for you?
At the moment I have to say YES :D

I did a test with ~25 series and populate each series with ~550.000 values. This worked so far.
Will do some other tests but for now it seems to be ok.

Thx for your help.

Dominik

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Thread & Repaint problems

Post by Christopher » Wed Jan 15, 2014 4:14 pm

moelski.net wrote: At the moment I have to say YES :D
Excellent :)
moelski.net wrote: Thx for your help.
You're very welcome.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply