Window not fully painting until I grab and move it

Viewed 88

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?

1 Answers

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.

Related