There's some strange mistake with timer and forms.
I am making editor for game. Editor has two forms - MainForm
and PreviewForm
. PreviewForm
contains only control for OpenGL output (Custom control based on GLControl from OpenTK), named glSurface
.
glSurface has two inline timers (Windows.Forms.Timer
) - one for rendering, and one for updating game state. Timers fires in glSurface
method Run(double updateRate, double frameRate)
.
So, I want to show PreviewForm
and run updating and rendering from MainForm
.
My code is:
PreviewForm = new PreviewForm();
PreviewForm.glSurface.Run(60d, 60d);
PreviewForm.Show(this); //Form is "modal"
Body of Run
method:
if (Running)
throw new Exception("Already run");
_updateRate = updateRate;
_renderRate = frameRate;
var renderFrames = Convert.ToInt32(1000/frameRate);
var updateFrames = Convert.ToInt32(1000/updateRate);
RenderTimer.Interval = renderFrames;
UpdateTimer.Interval = updateFrames;
RenderTimer.Start();
UpdateTimer.Start();
Running = true;
Timers is being initialized in OnVisibleChanged event:
protected override void OnVisibleChanged(EventArgs e)
{
...
RenderTimer = new Timer();
UpdateTimer = new Timer();
RenderTimer.Tick += RenderTick;
UpdateTimer.Tick += UpdateTick;
...
}
Weird things start here.
When PreviewForm
is showing, nothing happens. BUT when I close that form, both timers fire their events! I have tested for possible cross-thread interaction, but PreviewForm.InvokeRequired
and glSurface.InvokeRequired
are both false
.
Please help me find out what the hell is going on.