Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (158).NET Core  (28).NET MAUI  (44)Angular  (65)ASP.NET  (50)ASP.NET Core  (77)ASP.NET MVC  (88)Azure  (32)Blazor  (141)DocIO  (23)Essential JS 2  (80)Essential Studio  (171)File Formats  (27)Flutter  (121)JavaScript  (172)Microsoft  (112)PDF  (66)React  (52)Succinctly series  (126)Syncfusion  (571)TypeScript  (30)Uno Platform  (3)UWP  (4)Vue  (35)Webinar  (30)Windows Forms  (58)WinUI  (43)WPF  (134)Xamarin  (149)XlsIO  (25)Other CategoriesBarcode  (5)BI  (29)Bold BI  (4)Build conference  (6)Business intelligence  (53)Button  (4)C#  (117)Chart  (56)Cloud  (11)Company  (445)Dashboard  (7)Data Science  (3)Data Validation  (6)DataGrid  (54)Development  (354)Doc  (7)DockingManager  (1)eBook  (95)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (23)Extensions  (17)File Manager  (4)Gantt  (11)Gauge  (9)Git  (4)Grid  (27)HTML  (9)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (289)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (29)Performance  (9)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (7)Road Map  (10)Scheduler  (36)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (21)Solution Services  (2)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (89)UI  (200)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (1)Visual Studio  (23)Visual Studio Code  (14)Web  (326)What's new  (193)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Implementing CPU-Bound Operations in an ASP.NET Core Application

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

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 and 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 with the help of the keywords async and await associated with them.

The await operations can be split into:

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

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.

OutputGitHub reference

Find the complete example at Perform CPU-bound operations in ASP.NET Core 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 library, powered by Essential JS 2, 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. If not, you can download a free 30-day trial to evaluate our products.

You can contact us through our support forumsupport portal, or feedback portal. As always, we are happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top