Implementing CPU-Bound Operations in an ASP.NET Core Application
Tags: dotnet, csharp, webdev, productivity
Published: 2022-07-13 12:02:37

---
title: Implementing CPU-Bound Operations in an ASP.NET Core Application
published: true
date: 2022-07-13 10:30:46 UTC
tags: dotnet, csharp, webdev, productivity
canonical_url: https://www.syncfusion.com/blogs/post/implementing-cpu-bound-operations-in-an-asp-net-core-application.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zy56mr4mvlu85skyzwv.png
---

An application that loads a huge volume of data at once will have performance issues like **high time consumption** and **expensive computations**. To optimize these workloads and effectively process data, we can use [CPU-bound](https://en.wikipedia.org/wiki/CPU-bound "Wikipedia page for CPU-bound operation") and [I/O-bound](https://en.wikipedia.org/wiki/I/O_bound "Wikipedia page for I/O-bound operation") operations in our app.

In this blog, we will see the advantages of using CPU-bound operations and how to implement them in an ASP.NET Core app.

## Synchronous and asynchronous operations

First, let’s see the differences between synchronous and asynchronous operations.

A synchronous operation follows the specified steps of an algorithm where it waits for the previous action to complete to start the next operation. Whereas an asynchronous operation executes each action independently, and the result of the previous action doesn’t affect the flow of the operation.

In other words, the asynchronous operation allows the operations to run parallel in multiple threads to complete a task. We can use asynchronous operations to fetch data from huge databases and other time-consuming actions.

You can identify an [asynchronous operation](https://en.wikipedia.org/wiki/Asynchronous_operation "Wikipedia page for asynchronous operation") with the help of the keywords **async** and **await** associated with them.

The [await](https://en.wikipedia.org/wiki/Async/await "Wikipedia page for Async/await") operations can be split into:

- [I/O-bound](#i/o-bound-operations)
- [CPU-bound operations](#cpu-bound-operations)

## I/O-bound operations

An I/O-bound await operation denotes the rate at which an operation is executed by the speed of the I/O system. You can await an operation that returns a Task or Task<T> using the async method.

The I/O-bound operation is very time-consuming, as the operating system (OS) has to manage the workload. Since each action is performed synchronously, it affects the UI and also affects the performance.

## CPU-bound operations

A CPU-bound operation denotes the rate at which an operation is executed by the speed of the CPU. In other words, the CPU takes control of all the processes. You can await an operation that is started in the background with the thread **Task.Run** method.

The CPU-bound operation makes use of all the available processors and executes parallel operations through multithreading instead of running it in the main thread. It executes operations in the background without affecting the UI.

### Advantages of CPU-bound operations

Advantages of using CPU-bound operations over I/O-bound operations:

- Increases the performance of the processor.
- Provides better efficiency in scheduling and multithreading processes.

## Example of CPU-bound operations

Let’s create an ASP.NET Core app and learn how to implement CPU-bound operations in it.

### Step 1: Create an example ASP.NET Core project.

First, create an ASP.NET Core project. To do so, open [Visual Studio](https://visualstudio.microsoft.com/ "Visual Studio"). Then, click on **Create a new project**.

In the **Create a new project** dialog, select the **ASP.NET Core Empty** option from the list and click **Next**.

Refer to the following image.

![Create a new ASP.NET Core project.](https://www.syncfusion.com/blogs/wp-content/uploads/2022/07/Create-ASP.NET-Core-project..png)

### Step 2: Add namespaces.

After creating the project, add the following namespaces to the project.

```csharp
using System;
using System.IO;
using System.Threading.Tasks;
```

### Step 3: Implement CPU-bound operations.

Let’s see how to return an employee’s salary using CPU-bound operations.

```csharp
namespace Cpu_bound_sample
{
 class Program
 {
  static void Main(string [] args)
  {
    Salary(213434);
    Discount();
  }
  private static void Discount()
  {
    Console.WriteLine("Started some synchronous task");
  }
 
  private static async void Salary(float value)
  {
    Console.WriteLine("Started CPU Bound asynchronous task on a background thread");
    //CPU bound operation is called here;
    await Task.Run(() => SavingText(value));
  }
  //This method runs in the background;
  private static async void SavingText(float value
  {
    //Finds the location path
    string path = @"D:\Cpu.txt";
    try
    {
      if (!File.Exists(path))
      {
         using (var myFile = File.Create(path))
         {
           //Writes the file:
           TextWriter tw = new StreamWriter(path, false);
           tw.WriteLine(value);
           tw.Close();
 
         }
 
     }
     else if (File.Exists(path))
     {
       using (var myFile = File.Create(path))
       {
         //writes the file:
         TextWriter tw = new StreamWriter(path, true);
         tw.WriteLine(value);
         tw.Close();
 
       }
     }
   }
   catch (Exception ex)
   {
   }
}
```

You’ll find the CPU-bound operation is called in the **Salary()** method, which allows this **SavingText** method (task) to run in the background. Without taking any instance of time, the **SavingText** method will be performed in parallel by using multiple threads. So, the required task is completed within a limited time.

We will get output like in the following screenshot.

## ![Output](https://www.syncfusion.com/blogs/wp-content/uploads/2022/07/Implementing-CPU-Bound-Operations-in-an-ASP.NET-Core-Application.png)GitHub reference

Find the complete example at [Perform CPU-bound operations in ASP.NET Core on GitHub](https://github.com/SyncfusionExamples/CPU-Bound-Operation-in-ASP.NET-Core "Performing CPU-bound operations in ASP.NET Core app demo on GitHub").

## Conclusion

Thanks for reading! The most efficient way to run an asynchronous task is achieved by handling it with a CPU-bound operation. Mostly, a CPU-bound operation can be scaled separately so it does not affect the rest of an app. So, try out the steps given in this blog and enjoy better productivity!

The Syncfusion [ASP.NET Core UI controls](https://www.syncfusion.com/aspnet-core-ui-controls "Syncfusion ASP.NET Core UI controls") library, powered by [Essential JS 2](https://www.syncfusion.com/javascript-ui-controls "Syncfusion Essential JS 2 UI Controls"), is the only suite you will ever need to build an app. It contains over 70 high-performance, lightweight, modular, and responsive UI controls in a single package. Use them to build stunning web apps!

If you’re already a Syncfusion user, you can download the [product setup](https://www.syncfusion.com/account/downloads/studio/ "License and download page of Essential Studio"). If not, you can download a free [30-day trial ](https://www.syncfusion.com/downloads "Free evaluation of the Syncfusion Essential Studio")to evaluate our products.

You can contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion support forum"), [support portal](https://support.syncfusion.com/ "Syncfusion support portal"), or [feedback portal](https://www.syncfusion.com/feedback/ "Syncfusion Feedback Portal"). As always, we are happy to assist you!