Sign in to follow this  
Washu

[.net] Application.DoEvents

Recommended Posts

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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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)

Share this post


Link to post
Share on other sites
It is not nonsense. It is the way thinks are done in the SDK examples.
It gives you full control of the messageloop, however, it is not 'pure' .net
Pick the one that fits the purpose...

Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this