[.net] Application.DoEvents

Started by
13 comments, last by ernow 19 years ago
For those of you who may be wondering exactly what Application.DoEvents does, I've gone through the .Net framework DLL's and disassembled this for you:

private void RunMessageLoopInner(int reason, ApplicationContext context)
{
      bool flag2;
      if ((reason == 4) && !SystemInformation.UserInteractive)
      {
            throw new InvalidOperationException(SR.GetString("CantShowModalOnNonInteractive"));
      }
      if (reason == -1)
      {
            this.SetState(8, false);
      }
      if (Application.ThreadContext.totalMessageLoopCount++ == 0)
      {
            Application.ThreadContext.baseLoopReason = reason;
      }
      ++this.messageLoopCount;
      if (reason == -1)
      {
            if (this.messageLoopCount != 1)
            {
                  throw new InvalidOperationException(SR.GetString("CantNestMessageLoops"));
            }
            this.applicationContext = context;
            this.applicationContext.ThreadExit += new EventHandler(this.OnAppThreadExit);
            if (this.applicationContext.MainForm != null)
            {
                  this.applicationContext.MainForm.Visible = true;
            }
      }
      Form form1 = this.currentForm;
      if (context != null)
      {
            this.currentForm = context.MainForm;
      }
      bool flag1 = false;
      if ((reason == 4) || (reason == 5))
      {
            flag1 = true;
            if ((this.currentForm != null) && this.currentForm.IsHandleCreated)
            {
                  SafeNativeMethods.IsWindowEnabled(new HandleRef(this.currentForm, this.currentForm.Handle));
            }
            this.BeginModalMessageLoop();
            this.currentForm.Visible = true;
      }
      try
      {
            if (this.messageLoopCount == 1)
            {
                  this.InstallWindowsFormsSyncContextIfNeeded();
            }
            if ((this.ComponentManager is Application.ComponentManager))
            {
                  flag2 = this.ComponentManager.FPushMessageLoop(this.componentID, reason, 0);
                  return;
            }
            flag2 = this.LocalModalMessageLoop(((reason == 2) ? null : this.currentForm));
            return;
      }
      finally
      {
            if (flag1)
            {
                  this.EndModalMessageLoop();
            }
            this.currentForm = form1;
            --Application.ThreadContext.totalMessageLoopCount;
            --this.messageLoopCount;
            if (this.messageLoopCount == 0)
            {
                  new NamedPermissionSet("FullTrust").Assert();
                  try
                  {
                        AsyncOperationManager.OperationSynchronizationContext = this.previousSyncContext;
                  }
                  finally
                  {
                        CodeAccessPermission.RevertAssert();
                        this.previousSyncContext = null;
                  }
            }
            if (reason == -1)
            {
                  this.Dispose(true);
            }
            else if ((this.messageLoopCount == 0) && (this.componentManager != null))
            {
                  this.RevokeComponent();
            }
      }
}
This method is the method that actually does the work of Application.DoEvents. It is in System.Windows.Forms.Application+ThreadContext (A subclass of Application).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement
That's exactly why I have put together a tight wrapper around basic windowing features of the WinAPI myself (CreateWindow, message-loop, most common messages etc.). It has no per-frame heap-allocation (at least CLR Profiler says so), even the event arguments are pooled and recycled (safes a lot of heap-allocations e.g. for mouse-events). For stand-alone game-windows it works great.

Regards,
Andre
Andre Loker | Personal blog on .NET
Eh...Washu...how did you get that code?

I don't really understand what's goign on [wink] But it's interesting anyway.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Well, i dont have a direct link, Promit, but simply google for Reflector, and look for a link that somehow describes reflector as a 'disassembler for .net code'. Download that, and load up any .net dll/exe, and simply dig away to your heart's content!
Failing that, you can simply use the provided ILDASM utility provided with the .Net SDK to view the MSIL source code for any .Net assembly, including the Framework DLLs.

it looks like assembly language but you can work out whats going on.

There's actually an article on .NET assembly decompilation on 4guysfromrolla if anyone's interested in the theory (and indeed application). There's links on how to obfuscate your code too, for the paranoid out there.
There is also a professional tool called 'salamander' , which allows you view/edit .NET DLL's/EXE's. Dont remember the company name, as it was shown to me by a friend who is a professional coder, which i am not. At any rate it seems to be very good, and quite expencive (800-1200 USD)
Or, if you just want to see the source, you can look at Rotor (www.sscli.net).
------http://www.livejournal.com/users/rain_is_wet/
Just use Lutz Roeder's Reflector:

http://www.aisto.com/roeder/dotnet/
inneressing, but anyone have some resources about how to get around using Application.DoEvents();?

This topic is closed to new replies.

Advertisement