使用 C# 在 .NET 4.0 中替换 Task.Run 的方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17607048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a replacement method for Task.Run in .NET 4.0 using C#?
提问by Ace Caserya
I got this program that gives me syntax error "System.Threading.Tasks.task does not contain a definition for Run."
我得到了这个程序,它给了我语法错误“System.Threading.Tasks.task 不包含运行的定义”。
I am using VB 2010 .NET 4.0 Any ideas? any replacements for Run in .net 4.0?
我正在使用 VB 2010 .NET 4.0 有什么想法吗?在 .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;
});
}
}
}
采纳答案by McGarnagle
It looks like Task.Factory.StartNew<T>is what you're after.
看起来这Task.Factory.StartNew<T>就是你所追求的。
return Task.Factory.StartNew<int>(() => {
// ...
return 1;
});
Since the compiler can infer the return type, this also works:
由于编译器可以推断返回类型,这也适用:
return Task.Factory.StartNew(() => {
// ...
return 1;
});
回答by Saroop Trivedi
I changed your code with Task.Factory.StartNewcheck detail link
我使用Task.Factory.StartNew检查详细信息链接更改了您的代码
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;
});
}
回答by NPSF3000
Read this: http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx
阅读:http: //blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx
It explains that Task.Run is basically a nice wrapper introduced in 4.5.
它解释了 Task.Run 基本上是 4.5 中引入的一个很好的包装器。
回答by Eugene Podskal
The highest voted answer, unfortunately, is not exactly correct:
Unfortunately, the only overloads for StartNew that take a TaskScheduler also require you to specify the CancellationToken and TaskCreationOptions. This means that in order to use Task.Factory.StartNew to reliably, predictably queue work to the thread pool, you have to use an overload like this:
Task.Factory.StartNew(A, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
不幸的是,只有采用 TaskScheduler 的 StartNew 重载还需要您指定 CancellationToken 和 TaskCreationOptions。这意味着为了使用 Task.Factory.StartNew 可靠地、可预测地将工作排队到线程池中,您必须使用像这样的重载:
Task.Factory.StartNew(A, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
So the closest thing to Task.Runin 4.0 is something like:
所以最接近Task.Run4.0 的是这样的:
/// <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);
回答by Karmakulov Kirill
In case you're using Microsoft.Bcl.Asynchere you go:
如果您在此处使用Microsoft.Bcl.Async,请执行以下操作:
return TaskEx.Run(() =>
{
Console.WriteLine("* starting {0} work", name);
Thread.Sleep(milliseconds);
Console.WriteLine("* {0} work one", name);
return 1;
});

