MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

The name 'Tasks' does not exists in current context

Following script to play a sound. Sound length 6 seconds which plays repeating for 6 seconds

Task.Factory.StartNew<int>(() =>
{
    while (isSoundOn)
    {
        player.Play();
        Thread.Sleep(6000);
    }
    return 1;
 });

Everything is working fine for .Net Framework 4 but I have need to build for .Net Framework 3.

When I use .Net Framework 3 then it shows following error

The name 'Tasks' does not exists in current context

What will be the solution. Thanks in advance.

Upvotes: 9

Views: 7965

Answers (3)

LUser
LUser

Reputation: 1194

I am adding this because I lost some time searching for this.... All I had to add was the following...

using System.Threading.Tasks;

Upvotes: 14

Hari Prasad
Hari Prasad

Reputation: 16956

Task Parallel Library introduced as part of .Net Framework 4.

You can use Thread instead.

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

}).Start();

Upvotes: 1

Rob
Rob

Reputation: 27357

Task.Factory was only introduced in .NET Framework 4 - So you'll need to write something like this:

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

Though it really depends what you're actually doing. It's possible you may not even need threads, and simply write:

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

Upvotes: 2

Related Questions