76

我得到了这个程序,它给出了语法错误“System.Threading.Tasks.task 不包含 Run 的定义。”

我正在使用 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;
            });
    }
}
}
4

4 回答 4

88

看起来Task.Factory.StartNew<T>就是你所追求的。

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

由于编译器可以推断返回类型,这也可以:

return Task.Factory.StartNew(() => {
    // ...
    return 1;
});
于 2013-07-12T03:42:26.707 回答
10

不幸的是,最高投票的答案并不完全正确

不幸的是,StartNew 唯一采用 TaskScheduler 的重载还要求您指定 CancellationToken 和 TaskCreationOptions。这意味着为了使用 Task.Factory.StartNew 可靠、可预测地将工作排队到线程池,您必须使用这样的重载:

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

所以最接近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);
}

可以像这样使用:

Task
    .Factory
    .StartNewOnDefaultScheduler(() => 
        result);
于 2017-01-30T16:01:39.930 回答
5

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;
            });
        }
于 2013-07-12T03:53:27.423 回答
3

如果您在此处使用Microsoft.Bcl.Async ,请执行以下操作:

return TaskEx.Run(() =>
{
    Console.WriteLine("* starting {0} work", name);
    Thread.Sleep(milliseconds);
    Console.WriteLine("* {0} work one", name);
    return 1;
});
于 2018-08-01T06:45:22.573 回答