Hey
My garbage collection timeline is now lovely and flat :)
I was too lazy to put the whole of Tom's render loop in, so I'll describe my simpler way. It's just as good as Tom's original because DoEvents only ends up getting called like 5 times during the life of the application (obviously if you move your window about, this will increase, but to no worse than before)
So this was my old render loop:
while (mainWindow.Created)
{
FullRender();
System.Windows.Forms.Application.DoEvents();
}
And this is my new render loop, done in just a few seconds:
while (mainWindow.Created)
{
while (AppStillIdle)
{
FullRender();
}
System.Diagnostics.Debug.WriteLine("Calling DoEvents.");
System.Windows.Forms.Application.DoEvents();
}
private bool AppStillIdle
{
get
{
SafeNativeMethods.Message msg;
return !SafeNativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}
So simple to plug in! You could do it right now...I'll be kind and give you the native methods, it wasn't so easy to complete these, I had to have a look at the SDK framework to find WindowMessage:
internal static class SafeNativeMethods
{
/// <summary>Window messages</summary>
public enum WindowMessage : uint
{
// Misc messages
Destroy = 0x0002,
Close = 0x0010,
Quit = 0x0012,
Paint = 0x000F,
SetCursor = 0x0020,
ActivateApplication = 0x001C,
EnterMenuLoop = 0x0211,
ExitMenuLoop = 0x0212,
NonClientHitTest = 0x0084,
PowerBroadcast = 0x0218,
SystemCommand = 0x0112,
GetMinMax = 0x0024,
// Keyboard messages
KeyDown = 0x0100,
KeyUp = 0x0101,
Character = 0x0102,
SystemKeyDown = 0x0104,
SystemKeyUp = 0x0105,
SystemCharacter = 0x0106,
// Mouse messages
MouseMove = 0x0200,
LeftButtonDown = 0x0201,
LeftButtonUp = 0x0202,
LeftButtonDoubleClick = 0x0203,
RightButtonDown = 0x0204,
RightButtonUp = 0x0205,
RightButtonDoubleClick = 0x0206,
MiddleButtonDown = 0x0207,
MiddleButtonUp = 0x0208,
MiddleButtonDoubleClick = 0x0209,
MouseWheel = 0x020a,
XButtonDown = 0x020B,
XButtonUp = 0x020c,
XButtonDoubleClick = 0x020d,
MouseFirst = LeftButtonDown, // Skip mouse move, it happens a lot and there is another message for that
MouseLast = XButtonDoubleClick,
// Sizing
EnterSizeMove = 0x0231,
ExitSizeMove = 0x0232,
Size = 0x0005,
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Message
{
public System.IntPtr hWnd;
public WindowMessage msg;
public System.IntPtr wParam;
public System.IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, System.IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
}
Hope that helps!