Thread: How exactly is control passed between the Runtime Host, the CLR, and the App. Code?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    How exactly is control passed between the Runtime Host, the CLR, and the App. Code?

    I was taught that the CLR and the FCL are the main components of .NET. I was also taught that when you execute a .NET application, the CLR compiles the IL code contained in the assembly using its just-in-time compiler and then runs the application. That's pretty much all I was taught concerning the CLR and that was the end of the story.

    I was content with that explanation but recently I've been trying to deepen my understanding of what really goes on behind the scenes and I ended up completely confused.

    This is what I sort of understand...

    When you run an .exe file containing .NET code, the shell executable invokes a piece of code wihin the application called the 'runtime-host'. This code creates a process, loads the CLR into the process, creates an application domain within the process, loads the actual application code into the application domain (in order to isolate it from other code), and finally it executes the application code within the application domain.

    I mentally represent the above sequence of steps with pseudo code that more or less looks like this:

    Code:
    runtime-host
    {
    
        Process process =  new Process();
    
        try{
    
        process.LoadCLR();
        process.CreateApplicationDomain(app-code.Main);
        process.CompileJIT();
        process.Execute();
    
        }catch (Exception e){ //Display runtime error message
        }finally{ process.Close();}
    }
    
    app-code
    {
        ...
        static void Main(){...}
    }
    Then, if the user closes the application, or if control reaches the endpoint of the Main method, or if an exception is thrown and that exception is either uncaught or uncatchable (such as a StackOverFlowException) control is passed up the call stack of the application code, and into the runtime-host until, in the case of an exception, it finds the above catch clause which displays a message indicating that an error occurred, and finally, exception or no exception, control is passed to the finally block which closes the process.

    That more or less sums up my 'understanding' of what goes on behind the scenes. Then again, I think it's obvious that I'm filling up lots of gaps with mere conjectures and simplifications so if someone can explain all this in a manner that I can easily understand I would appreciate it.
    Last edited by y99q; 12-04-2011 at 11:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. runtime error in code
    By Lenjaku in forum C Programming
    Replies: 3
    Last Post: 11-08-2010, 03:48 PM
  2. Change a control class during runtime
    By Joelito in forum Windows Programming
    Replies: 3
    Last Post: 01-12-2006, 02:13 PM
  3. Executing C++ Code at Runtime
    By jdm in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2004, 05:32 PM
  4. Load and run machine code at runtime
    By ninebit in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2002, 10:26 AM
  5. getting control type at runtime
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 11:23 AM