Jump to content
  • Advertisement
Sign in to follow this  
Washu

[.net] Application.DoEvents

This topic is 4829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
Eh...Washu...how did you get that code?

I don't really understand what's goign on [wink] But it's interesting anyway.

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

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
Guest Anonymous Poster
Just use Lutz Roeder's Reflector:

http://www.aisto.com/roeder/dotnet/

Share this post


Link to post
Share on other sites
inneressing, but anyone have some resources about how to get around using Application.DoEvents();?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!