Bug 9345 - async capturing of 'this' not working?
Summary: async capturing of 'this' not working?
Status: RESOLVED FIXED
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Debugger ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jeffrey Stedfast
URL:
Depends on:
Blocks:
 
Reported: 2013-01-07 17:06 UTC by Jeffrey Stedfast
Modified: 2013-01-08 10:33 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jeffrey Stedfast 2013-01-07 17:06:36 UTC
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncDebugging
{
	class MainClass
	{
		int foo = 0;

		public async Task<int> HelloWorldAsync ()
		{
			Stopwatch sw = Stopwatch.StartNew ();
			var task = Task<int>.Factory.StartNew (() => { Thread.Sleep (4 * 1000); return 10; });
			int result = await task;

			Console.WriteLine ("Hello, World! result = {0}; {1}ms", result, sw.ElapsedMilliseconds);
			return result + foo;
		}

		public static void Main (string[] args)
		{
			var app = new MainClass ();
			Task t = app.HelloWorldAsync ();
			t.Wait ();

			Console.WriteLine ("Done.");
		}
	}
}


If you set a breakpoint anywhere in HelloWorldAsync(), the variable 'this' does not seem to get captured (it doesn't appear in the "Locals" pad) when the breakpoint is hit.
Comment 1 Marek Safar 2013-01-08 04:51:44 UTC
It's captures otherwise the could would fail to run.

Looking at the generated code you can see

  .class nested private auto ansi sealed beforefieldinit '<HelloWorldAsync>c__async0'
  	extends [mscorlib]System.ValueType
  	implements [mscorlib]System.Runtime.CompilerServices.IAsyncStateMachine  {

    .field  assembly  class AsyncDebugging.MainClass $this
Comment 2 Jeffrey Stedfast 2013-01-08 10:33:40 UTC
fixed in git master, 3.1.x, and ui-refresh