19

I tried to implement multithreading in my code, 1st time. When i tried to use

Task T = Task.Run(() => { });

Visual Studio is still underlines Run() with statement "Task does not contain a definition 'Run' "

I'm using System.Threading.Tasks; Internet knows nothing about this problem

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
kifu
  • 193
  • 1
  • 1
  • 9

3 Answers3

25

.NET 4.0 does not have a Task.Run method. Instead you can use:

Task T = Task.Factory.StartNew(() => { });

Which you can learn more about here

Michael Hancock
  • 2,673
  • 1
  • 18
  • 37
12

Task.Run was introduced in .NET 4.5, you are using .net 4.0. If you can't upgrade your project you can include the Microsoft.Bcl.Async NuGet package to introduce a TaskEx.Run( to add it in to .net 4.0.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I was using .net framework 4.5 but it still said that it doesn't contain any method called `Run`.. Now I upgraded to 4.7 and still the same error. So it's not only the .net version. it must be something else. can anybody help me? – slow May 24 '19 at 09:04
  • You will need to ask a separate question with a full code sample that reproduces the problem we can copy and paste. Recreate the problem on https://dotnetfiddle.net/ and include a link to the code in your question. Make sure you have the full code in the question too. – Scott Chamberlain May 24 '19 at 09:08
  • ah, I just figured it out. i was using the namespace `Microsoft.Exchange.WebServices.Data` Which had the class `Task`. So It was referring to this Task instead of `System.Threading.Tasks.Task`. Common mistake. Thanks anyway ;) – slow May 24 '19 at 09:09
8

Unfortunately, is not exactly correct that Task.Run is same as Task.Factory.StartNew.

In this answer , it is given the closest thing to Task.Run in 4.0 which is something like:

/// <summary>
/// Starts the new <see cref="Task"/> from <paramref name="function"/> on the Default(usually ThreadPool) task scheduler (not on the TaskScheduler.Current).
/// It is a 4.0 method nearly analogous to 4.5 Task.Run.
/// </summary>
/// <typeparam name="T">The type of the return value.</typeparam>
/// <param name="factory">The factory to start from.</param>
/// <param name="function">The function to execute.</param>
/// <returns>The task representing the execution of the <paramref name="function"/>.</returns>
public static Task<T> StartNewOnDefaultScheduler<T>(this TaskFactory factory, Func<T> function)
{
    Contract.Requires(factory != null);
    Contract.Requires(function != null);

    return factory
        .StartNew(
            function,
            cancellationToken: CancellationToken.None,
            creationOptions: TaskCreationOptions.None,
            scheduler: TaskScheduler.Default);
}

that can be used like:

Task
    .Factory
    .StartNewOnDefaultScheduler(() => 
        result);

But Task.Run is a nice wrapper over Task.Factory.StartNew introduced in .NET 4.5.

But for your situation, when you need it for multithreading operations, just use it.

But don't forget.

You should prefer Task.Run over Task.Factory.StartNew if you use async code.

In this article , Stephen Curry gives two reason that StartNew method is dangerous:

  • Does not understand async delegates.
  • Confusing default scheduler.
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • What is the world is `Contract`? `The name 'Contract' does not exist in the current context` – Alex G May 08 '20 at 17:44