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.
I am adding this because I lost some time searching for this.... All I had to add was the following...
using System.Threading.Tasks;
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);
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With