Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 221
MaxEcho Avatar asked Mar 21 '16 04:03

MaxEcho


3 Answers

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

using System.Threading.Tasks;
like image 50
LUser Avatar answered Oct 31 '22 15:10

LUser


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);
}
like image 37
Rob Avatar answered Oct 31 '22 17:10

Rob


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();
like image 41
Hari Prasad Avatar answered Oct 31 '22 17:10

Hari Prasad