I tried to implement multithreading in my code, 1st time. When i tried to use
Task T = Task.Run(() => { });
Visual Studio is still underlines Run() with statement "Task does not contain a definition 'Run' "
I'm using System.Threading.Tasks; Internet knows nothing about this problem
The Run method allows you to create and execute a task in a single method call and is a simpler alternative to the StartNew method. It creates a task with the following default values: Its cancellation token is CancellationToken.
A task in C# is used to implement Task-based Asynchronous Programming and was introduced with the . NET Framework 4. The Task object is typically executed asynchronously on a thread pool thread rather than synchronously on the main thread of the application.
Differences Between Task And ThreadThe Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel. The task can return a result.
Cancel(); Console. WriteLine("\n\nPress any key to stop the task"); Console.
.NET 4.0
does not have a Task.Run
method. Instead you can use:
Task T = Task.Factory.StartNew(() => { });
Which you can learn more about here
Task.Run was introduced in .NET 4.5, you are using .net 4.0. If you can't upgrade your project you can include the Microsoft.Bcl.Async NuGet package to introduce a TaskEx.Run(
to add it in to .net 4.0.
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