.net - Why does the Visual Studio debugger not show the state machine code generated by the await keyword? -


when set break-point body of method awaiting, visual studio debugger shows call stack not have of c# compiler generated code.

please @ picture below, shows simple async method , call stack.

enter image description here

notice there no call movenext or initialize state machine object or taskawaiter<tresult> or asynctaskoftmethodbuilder in call stack.

why so? there in il spy, there setting in visual studio can toggle have show state machine code in call stack , in editor while debugging session in progress?

i remembered there debuggerstepthroughattribute on re-written button click handler code instantiates state machine. not sure if reason hidden debugger. shouldn't 1 grey out stack frame of compiler generated methods in call stack?

isn't debuggerhiddenattribute 1 makes code invisible debugger?

there's couple of things note here.

first of all, have 2 threads in play:

  1. the main thread, running message pump , owning window has button in it
  2. the thread running task

the call stack inside 1 thread in no way reflect thread created doing.

at most, see 2 threads , callstack if inspect debugger pane can show threads , callstacks in debugger.

however, show button event handler if thread executed method still in method @ time debugger hit breakpoint. not true due overhead of spinning new thread.

the reason async/await returns async method when hits await. time separate thread had started executing delegate, button event handler method has returned.

even if manually spun thread delegate , joined (waited for) thread, wouldn't see in callstack, should see in other debugger pane threads , callstacks.