This blog is subject the DISCLAIMER below.

Thursday, February 15, 2007

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()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ShowMeWhatHappend();
}
/// 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();
listBox1.Items.Add(MB.Name);
counter++;
}
label1.Text += counter;
}

23, because of ShowMeWhatHappend calling.

11 comments:

Mohamed Gamal El-Din said...

Great .. Great , thanks Romio

Ramy Mahrous said...

welcome, Gamal, hope it added something new to you

Mohamed Moshrif said...

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

Ramy Mahrous said...

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

Islam Ossama said...

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.

Shereef Sakr said...
This comment has been removed by the author.
Shereef Sakr said...

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.

http://blogs.msdn.com/jmstall/archive/2005/03/20/399287.aspx

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

Ramy Mahrous said...

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

Ramy Mahrous said...

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

Shereef Sakr said...

Here is the link in two parts

http://blogs.msdn.com/jmstall
/archive/2005/03/20/399287.aspx

Mohamed Moshrif said...

@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