What is the substitution method for Task.Run in .NET 4.0 using C #?

I got this program which gives me the syntax error "System.Threading.Tasks.task does not contain a definition for Run".

I am using VB 2010.NET 4.0 Any ideas? any replacements for Run in.net 4.0?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ChatApp { class ChatProg { static void Main(string[] args) { Task<int> wakeUp = DoWorkAsync(2000,"Waking up"); Task.WaitAll(wakeUp); } static Task<int> DoWorkAsync(int milliseconds, string name) { //error appears below on word Run return Task.Run(() => { Console.WriteLine("* starting {0} work", name); Thread.Sleep(milliseconds); Console.WriteLine("* {0} work one", name); return 1; }); } } } 
+66
c # asynchronous task-parallel-library
Jul 12 '13 at 3:29
source share
6 answers

It looks like Task.Factory.StartNew<T> is what you need.

 return Task.Factory.StartNew<int>(() => { // ... return 1; }); 

Since the compiler can infer a return type, this also works:

 return Task.Factory.StartNew(() => { // ... return 1; }); 
+77
Jul 12 '13 at 3:42 on
source share

Read this: http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx

It explains that Task.Run is basically a nice shell introduced in 4.5.

+34
Jul 12 '13 at 3:58
source share

The highest voted answer , unfortunately, is not entirely correct :

Unfortunately, the only overloads for StartNew that accept TaskScheduler also require that you specify CancellationToken and TaskCreationOptions. This means that in order to use Task.Factory.StartNew to work reliably with the predicted queue in the thread pool, you need to use the following overload:

Task.Factory.StartNew (A, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

So, the closest thing to Task.Run in 4.0 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); } 

which can be used as:

 Task .Factory .StartNewOnDefaultScheduler(() => result); 
+5
Jan 30 '17 at 16:01
source share

I changed your code using Task.Factory.StartNew check detail link

  static Task<int> DoWorkAsync(int milliseconds, string name) { //error appears below on word Run return Task.Factory.StartNew(() => { Console.WriteLine("* starting {0} work", name); Thread.Sleep(milliseconds); Console.WriteLine("* {0} work one", name); return 1; }); } 
+3
Jul 12 '13 at 3:53 on
source share

If you use Microsoft.Bcl.Async here, you go:

 return TaskEx.Run(() => { Console.WriteLine("* starting {0} work", name); Thread.Sleep(milliseconds); Console.WriteLine("* {0} work one", name); return 1; }); 
0
Aug 01 '18 at 6:45
source share
 Task.Factory.StartNew(() => { try { Thread.Sleep(200); IntPtr TouchhWnd = IntPtr.Zero; TouchhWnd = FindWindow(null, "屏幕键盘"); if (TouchhWnd != IntPtr.Zero) { SetWindowPos(TouchhWnd, HWND_TOPMOST, 500, 500, 400, 300, SWP_SHOWWINDOW); } } catch { } }); 
-5
09 Oct '15 at 3:42
source share



All Articles