Bug 5298 - Task.Wait sometimes executes task on same thread.
Summary: Task.Wait sometimes executes task on same thread.
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-05-23 23:14 UTC by Peter Ritchie
Modified: 2012-05-30 07:16 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 GitHub or Developer Community 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 Peter Ritchie 2012-05-23 23:14:26 UTC
the following test fails consistently in Windows 7.

		[Test]
		public void TaskWaitDoesNotRunTaskOnCurrentThread()
		{
			var mainThreadId = Thread.CurrentThread.ManagedThreadId;
			var threadThreadId = mainThreadId;
			Task task = Task.Factory.StartNew(() => threadThreadId = Thread.CurrentThread.ManagedThreadId);
			task.Wait();
			task = Task.Factory.StartNew(() => threadThreadId = Thread.CurrentThread.ManagedThreadId);
			task.Wait();

			Assert.AreNotEqual(mainThreadId, threadThreadId);
		}

This same test with .NET 4.0 does not fail.
Comment 1 Marek Safar 2012-05-29 11:56:14 UTC
This is implementation/OS detail. .NET Framework is free to choose any available thread to execute the task
Comment 2 Peter Ritchie 2012-05-29 13:44:12 UTC
Any *available* thread, it's running it on the *current* thread.

If you're trying to do background tasks *off* the UI thread, this is bad; because the code ends up running on the UI thread and completely bogs down the UI.
Comment 3 Marek Safar 2012-05-30 07:16:57 UTC
Fixed in master, the handling is now task scheduler specific.

Thanks for the bug report.