FCI-Helwan blog

Just another FCI-H weblog

What happens behind the scene??!

Did you think a day what happens when you create a windows application and run it?

Did you think what function(s) called to show you the form with the controls associated with it?

I know I know, you think or thought in that… me too, back to old days, I remembered something, Shreef Sakr and I were working on Visual Studio.net 2003 in our FCI-H lab. I noticed that Shreef writing some lines of code, I’ve attracted then I’ve asked him what’s this?

He replied “I saw in MSDN this class StackTrace, and I am still discovering it”

Today in the meeting with Hanaa, I remembered this class and I decided to write about it.

No…, I did not forget, did you think a day how many functions should be called to show you a simple form? so think, if you did not. Guess!…. your answer may be 1, 2, 3 or even 4 functions, but my answer is main, Run, form constructor, addcontrols, initializecomponent and load.

We got the big zero :-), although C# is not open source but you can easily know what’s happen behind the scene and StackFrame can remove the curtain to see, so the answer is little weird. To show a simple form, just 22 functions called.

Show me!
public Form1()

private void Form1_Load(object sender, EventArgs e)
/// Shows me the name of functions called in the stack frame till execution of
/// ShowMeWhatHappend
public void ShowMeWhatHappend()
int counter = 0;
System.Diagnostics.StackTrace ST = new StackTrace();
System.Diagnostics.StackFrame[] SF = ST.GetFrames();
foreach (StackFrame sf in SF)
System.Reflection.MethodBase MB = sf.GetMethod();
label1.Text += counter;

23, because of ShowMeWhatHappend calling.


February 15, 2007 - Posted by | C#


  1. Great .. Great , thanks Romio

    Comment by Mohamed Gamal El-Den | February 15, 2007 | Reply

  2. welcome, Gamal, hope it added something new to you

    Comment by Ramy Mahrous | February 16, 2007 | Reply

  3. I think it may be more than this, I am not sure whether the StackTrace class is showing the transitions between managed/unmanaged calls, but from what I see it’s not, so there may be a few more calls, I may check this when I get some time 🙂

    Comment by Mohamed Meshref | February 16, 2007 | Reply

  4. No, it works, you won’t find a good documentation for it in MSDN, but if you want me to give you the sample I made, tell me

    Comment by Ramy Mahrous | February 16, 2007 | Reply

  5. You can also watch the stack trace from Visual Studio when you are debugging your program, it will show you the same as what is demonstrated here, and if you have a large program you can trace back and forth between function calls while watching who called which functions. This is all available through the stack trace window in Visual Studio, and almost every other debugger for any language would probably have a similar feature.

    And yeah, I agree with Mohamed Meshref, 22 is too little, it’s probably more than that, because some functions are called into and returned from before reaching the Form_Load function.

    Comment by Islam Ossama | February 16, 2007 | Reply

  6. It’s probably not showing all functions calls cuz if u debug any VC++ program, it will show you some function calls which are not resolved to a name. It just displays the address.

    btw StackTrace doesn’t show CLR internal frames.

    Also, the compiler may inline some function calls when not in debug mode.


    But anyway StackTrace serves the purpose i don’t think we need more.

    Comment by Shereef Sakr | February 16, 2007 | Reply

  7. @Islam
    Form1_Load is the last function called regardless ShowMeWhatHappend
    and the listbox displayed them as are.
    So, you’ll find Main the first, then Run, RunMessageLoop, etc….

    Comment by Ramy Mahrous | February 16, 2007 | Reply

  8. @Shreef
    Thank you, for the link but it does not work, google may cut it.

    Comment by Ramy Mahrous | February 16, 2007 | Reply

  9. Here is the link in two parts


    Comment by Shereef Sakr | February 16, 2007 | Reply

  10. @Islam Ossama: you want see the whole stack in VS except if you disabled “Show only my code” from the debug options which is enabled by default

    Comment by Mohamed Meshref | March 7, 2007 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: