5

I recently added some charts to a WinForm that I am updating with live data. My form load likes like:

private void MyForm_Load (object sender, EventArgs e){
...bunch of code
//Waveforms
InitializeChartEKG();
InitializeSpO2WaveformChart();
InitializeEtCO2WaveformChart();
FillWaveformCharts();
}

The live charts are created like so:

public async void FillWaveformCharts() {
     waveformData = AnesthWaveformDatas.CreateWaveformObjects(anestheticRecordNum);
     Action action = () =>
     FillChartEKG(waveformData);
     Invoke(action);
     action = () =>
     FillChartSpO2(waveformData);
     Invoke(action);
     action = () =>
     FillChartEtCO2(waveformData);
     Invoke(action3);
}

And then, in each FillChart method, I update the UI so the form doesn't freeze:

public async void FillChartEKG (List<AnesthWaveformData> waveformData) {
 for (i = 0; i < waveformData.Count; i++){
   ...bunch of code that takes a long time to run...
  UpdateUI(); //updates UI to keep form from freezing

}

And finally, UpdateUI(), which updates the UI to keep the form from freezing while the live data is being updated in the charts:

private void UpdateUI () {
   DispatcherFrame frame = new DispatcherFrame();
   Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new 
   DispatcherOperationCallback(delegate (object parameter) {
     frame.Continue = false;
     return null;
   }), null);
   Dispatcher.PushFrame(frame);
}

So this all works pretty nicely - my charts update with live data streaming from right to left and the form doesn't freeze at all. The only problem is, that 3/4 of the form's controls don't paint on Form_Load. If I grab the title bar and move the window, the controls will paint instantly.

I have tried:

this.Invalidate();
this.Update();

and:

this.Refresh();

after the FillWaveforms() method which doesn't work.

Here is what I see on load:

Unpainted

And here is what it looks like if I grab the title bar and move the window a bit:

Painted

Any ideas?

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • It's probably worth iteratively commenting out the new code and seeing what bit introduces the problem. From the symptoms, it seems like a corrupted graphics context or manipulating something from the wrong thread. If your moving slow code to another thread, you need to make sure the correct thread is the one that draws the result. – Charlie Nov 10 '21 at 06:06
  • 1
    It looks for my similar to [Redraw issue on Windows10 with DoubleBuffering and FormBorderStyle.None](https://stackoverflow.com/questions/51824224/redraw-issue-on-windows10-with-doublebuffering-and-formborderstyle-none) – Rekshino Nov 10 '21 at 07:11

1 Answers1

2

Thanks @Rekshino that's it. Looks like I stumbled across a Windows bug.

Adding

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
        return cp;
    }
}

To the top of my Form as per that link your provided solved the problem. Thanks.