3

I am currently using .NET 4 with Visual Studio 2010. MSDN said that Task.Delay is not available in this version of .NET framework.

However, in my system ILdasm shows Task.Delay do exists in mscorlib (4.0.0.0). So I tested using reflection:

typeof (Task).GetMethod("Delay", new []{typeof (int)})
    .Invoke(null, new object[]{1000});

This seems to works on my computer. So my questions are:

  1. Is this means Microsoft provided that method somehow, but they are just hiding it?

  2. Is it safe to have a wrapper function to use it in above way? More precisely, would this work on other computers that only have .NET 4 installed? (my system is Windows 7)

UPDATE

I forgotten weather or not I had .NET 4.5 installed. So I checked from control panel. Yes, there is .NET 4.5.1 installed.

Earth Engine
  • 10,048
  • 5
  • 48
  • 78

2 Answers2

11

This method does not exist in .NET 4.0.

You are probably ILspying 4.5. All of .NET 4.x's assemblies, through 4.5.2 at the time of this post, show v4.0.0.0, and newer versions replace older versions when you install them -- they're made to be backward compatible.

When you are able to grab Task.Delay through reflection, it's because your app is actually running on .NET 4.5.

When you select a framework version in Visual Studio, it will use whatever version you have installed, but only show the methods actually in that selection -- you can think of it as a compatibility selection, not so much an exact version selection.

I believe one of the Microsoft.Bcl and Microsoft.Bcl.Async packages on NuGet contains a TaskEx.Delay you can use which will emulate it on older frameworks and route to the build-in version when used on newer ones. These packages contain back-ported APIs.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • Is that means my program is also running under .NET 4.5 (and actually it is this version in use by VS2010)? Since my code above is running directly under Visual Studio 2010. – Earth Engine Jul 09 '14 at 04:59
  • `Microsoft.Bcl.Async` requires VS2012+ though. – noseratio Jul 09 '14 at 06:42
  • 1
    @Noseratio Not quite. You can use `Microsoft.Threading.Task.TaskEx` extension in VS2010, but you cannot use `async` things without VS2012+ – Earth Engine Jul 09 '14 at 11:33
  • @EarthEngine, I wouldn't use it with VS2010 as the the [package's page](http://www.nuget.org/packages/Microsoft.Bcl.Async/) states this explicitly: *This package is not supported in Visual Studio 2010...* – noseratio Jul 09 '14 at 11:39
  • Yes, .NET 4.5 replaces .NET 4.0, so VS2010 and your app will actually be running on 4.5 if you have it installed. – Cory Nelson Jul 09 '14 at 14:24
0

If its not documented, you have no guarantee that it will work consistently or work at all. But you know that already :) They kept it not public because they either didn't have time to adequately test/fix it or they weren't convinced at the time that the API needed to exist. Either way, the question you should be asking is how to implement your own version of this API using the tools that do exist in .NET4

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • Btw here's the .net 4 docs which show that .delay is indeed not listed http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.100).aspx – Robert Levy Jul 09 '14 at 04:46