Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称';任务';在当前上下文中不存在_C#_.net Framework Version - Fatal编程技术网

C# 名称';任务';在当前上下文中不存在

C# 名称';任务';在当前上下文中不存在,c#,.net-framework-version,C#,.net Framework Version,按照脚本播放声音。声音长度6秒,重复播放6秒 Task.Factory.StartNew<int>(() => { while (isSoundOn) { player.Play(); Thread.Sleep(6000); } return 1; }); Task.Factory.StartNew(()=> { while(伊索顿) { player.Play(); 睡眠(6000); } 返回1; });

按照脚本播放声音。声音长度6秒,重复播放6秒

Task.Factory.StartNew<int>(() =>
{
    while (isSoundOn)
    {
        player.Play();
        Thread.Sleep(6000);
    }
    return 1;
 });
Task.Factory.StartNew(()=>
{
while(伊索顿)
{
player.Play();
睡眠(6000);
}
返回1;
});
对于.Net Framework 4,一切都很正常,但我需要为.Net Framework 3构建

当我使用.NETFramework 3时,它会显示以下错误

当前上下文中不存在名称“任务”


解决办法是什么。提前感谢。

任务。Factory仅在中介绍-因此您需要编写如下内容:

var thread = new Thread(() =>
{
    while (isSoundOn)
    {
        player.Play();
        Thread.Sleep(6000);
    }
});
thread.Start();
thread.Join();
尽管这取决于你到底在做什么。您甚至可能不需要线程,只需编写:

while (isSoundOn)
{
    player.Play();
    Thread.Sleep(6000);
}

作为的一部分引入的任务并行库

您可以使用
线程

new Thread(() =>
{
    while (isSoundOn)
    {
        player.Play();
        Thread.Sleep(6000);
    }    

}).Start();

我添加这个是因为我在搜索这个时浪费了一些时间。。。。 我要补充的是以下内容

using System.Threading.Tasks;

谢谢,我会的。我会试试的。谢谢