Violet_CLM's picture

Update two GLControls in a single event

I have a form with two GLControls. If a single event (e.g. moving a scrollbar) calls Invalidate() on one of them, it gets updated. However, if a single event calls Invalidate() on both of them, neither of them update at all. Based on adding various debug actions, code seems to stall at the MakeCurrent lines and the remaining lines within the Paint functions are simply not executed. Here's a fairly minimal example of the issue: I have a form with two GLControls "glControl1" and "glControl2," plus a button "button1." Events handlers are attached to all shown functions. What am I doing wrong?

using System;
using System.Drawing;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }
 
        private void button1_Click(object sender, EventArgs e)
        {
            glControl1.Invalidate();
            glControl2.Invalidate();
        }
 
        private void glControl1_Load(object sender, EventArgs e) { Setup(glControl1); }
        private void glControl2_Load(object sender, EventArgs e) { Setup(glControl2); }
        private void Setup(GLControl Control)
        {
            glControl1.Context.MakeCurrent(Control.WindowInfo);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, Control.Width, 0, Control.Height, -1, 1);
            GL.Viewport(Control.ClientRectangle);
            GL.ClearColor(Color.BurlyWood);
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            glControl1.Context.MakeCurrent(glControl1.WindowInfo);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            glControl1.SwapBuffers();
        }
 
        private void glControl2_Paint(object sender, PaintEventArgs e)
        {
            glControl1.Context.MakeCurrent(glControl2.WindowInfo);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            glControl1.SwapBuffers();
 
        }
    }
}