Manage Program Flow - Implement multithreading and asynchronous processing - Using Tasks

6 important questions on Manage Program Flow - Implement multithreading and asynchronous processing - Using Tasks

A task can be use to make your application more .........

Responsive.

How to create a task and immediatly start it?


Task t = Task.Run(() =>
{
for (int x = 0; x < 100; x++)
{
Console.Write(‘Deze tekst wordt nu 100 x afgedrukt in de console’);
}
});

When is Task<T> used?

When Task should return a value.


Task<int> t = Task.Run(() =>
{
                 return 42;
});

Console.WriteLine(t.Result); // Displays 42
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

What is a Task object?

A Task object encapsulates a job that needs to be executed. Tasks are the recommended way to create multithreaded code.

What is Task.Run?

Create a task and immediatly run it on a different thread (in principe maak je hiermee de taak asynchroon).
Example:
Task t = Task.Run(() => {blablabla});

Name the properties of the task class.

Exception - Returns any exeptions that caused the task to end early

Status:  Returns the Tasks status

IsCancelled: Returns true if the Task was cancelled

IsCompleted: Returns true if the task is completed succesfully

IsFaulted: Returns true if the task is stopped due to an unhandled                              exception

Factory: Provides access to TaskFactory class. You can use that to                             create Tasks

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo