---
title: "Implementing CPU-Bound Operations in an ASP.NET Core Application"
published_at: "2022-07-13T10:30:46+00:00"
modified_at: "2026-01-09T14:02:52+00:00"
url: "https://www.syncfusion.com/blogs/post/implementing-cpu-bound-operations-in-an-asp-net-core-application"
excerpt: "https://ft.syncfusion.com/blogs-pdf/docs/implementing-cpu-bound-operations-in-an-asp-net-core-application.pdfAn 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 and I/O-bound operations in our app...."
taxonomy_category:
  - "ASP.NET Core"
  - "Web"
taxonomy_post_tag:
  - "ASP.NET Core"
  - "productivity"
  - "Web Development"
---

# Implementing CPU-Bound Operations in an ASP.NET Core Application

[Vivek Sankar](https://www.syncfusion.com/blogs/author/vivek-sankar)

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


https://ft.syncfusion.com/blogs-pdf/docs/implementing-cpu-bound-operations-in-an-asp-net-core-application.pdfAn 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)
 and [I/O-bound](https://en.wikipedia.org/wiki/I/O_bound)
 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)
 with the help of the keywords **async** and** await** associated with them.

The [await](https://en.wikipedia.org/wiki/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/)
. 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.

```
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.

```
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)
.

## 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)
 library, powered by [Essential JS 2](https://www.syncfusion.com/javascript-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/)
. If not, you can download a free [30-day trial](https://www.syncfusion.com/downloads)
to evaluate our products.

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

## Related blogs

- [Simple Steps to Automate UI Testing in ASP.NET Core](https://www.syncfusion.com/blogs/post/simple-steps-to-automate-ui-testing-in-asp-net-core.aspx)
- [Hosting Multiple ASP.NET Core Apps in Ubuntu Linux Server Using Apache](https://www.syncfusion.com/blogs/post/hosting-multiple-asp-net-core-apps-in-ubuntu-linux-server-using-apache.aspx)
- [Easy Steps to Migrate an ASP.NET MVC Project to an ASP.NET Core Project](https://www.syncfusion.com/blogs/post/migrate-asp-net-mvc-project-to-asp-net-core.aspx)
- [How to Apply API Versioning in ASP.NET Core](https://www.syncfusion.com/blogs/post/how-to-apply-api-versioning-in-asp-net-core.aspx)
