Skip to main content

Command Palette

Search for a command to run...

What distinguishes synchronous from asynchronous threads in C#?

Published
3 min read
F
I am an Infrastructure and DevOps Engineer specializing in designing, building, and operating scalable, secure, and highly available cloud infrastructure. My core focus is on Microsoft Azure cloud platforms, Infrastructure as Code (IaC), and DevOps automation to support reliable production systems. I work across cloud infrastructure engineering, DevOps practices, and site reliability engineering (SRE) principles to ensure systems are resilient, observable, and optimized for performance, cost, and scalability. My experience includes designing and managing cloud environments across compute, networking, storage, identity, and security layers. I build Infrastructure as Code solutions using Terraform and Azure Resource Manager (ARM) templates to automate provisioning, configuration, and deployment of cloud resources. I am actively involved in improving system reliability through monitoring, logging, and incident response processes using tools such as Azure Monitor and cloud-native observability solutions. I also participate in on-call operations, production support, and incident management to ensure high availability of critical systems. Security is a core part of my engineering approach. I work with identity and access management (IAM), Azure Active Directory, and cloud security best practices to ensure infrastructure remains compliant, secure, and audit-ready in line with industry standards such as ISO 9001 and ISO 27001. I collaborate with cross-functional teams including software engineers, DevSecOps, and product teams to deliver infrastructure solutions for customer-facing applications and enterprise platforms. My technical interests and growth areas include: Cloud Infrastructure Engineering (Azure, AWS, GCP) Site Reliability Engineering (SRE) Platform Engineering Kubernetes & Container Orchestration Infrastructure as Code (Terraform, ARM) CI/CD Pipeline Automation Distributed Systems & System Design Cloud Security & Identity Management I am passionate about building systems that are not only scalable and efficient but also reliable and easy for engineers to use. I am continuously growing my expertise toward senior-level Infrastructure, SRE, and Platform Engineering roles, including global remote opportunities.

What is synchronized, and how does C# use it?

Methods in C# always run synchronously by default. One synchronous operation task at a time, and only after that task is finished is the next unblocked. In other words, you must wait for a task to be completed before going on to the next.

Code Sample:

using System;

namespace CSharpSynchronousExample
{
    class Program
    {
        public static void Main(string[]args)
        {
            Console.WriteLine("OUTPUT");

            GetStudentName();
            GetStudentNumber();
            Console.ReadLine();
        }

        private static void GetStudentName()
        {
            Task.Delay(1000);
            Console.WriteLine("Student Name is Samuel");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "1";

            Thread currentThread = Thread.CurrentThread;
            Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }

        private static void GetStudentNumber()
        {
            Task.Delay(1000);
            Console.WriteLine("Student Number is 12");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "2";

        Thread currentThread = Thread.CurrentThread;
        Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }
    }
}

Output

AC.png

What is asynchronous programming and how does C# use it?

On the other hand, using asynchronous operations, you can switch to a new task before the previous one is complete. This allows you to handle several requests concurrently and finish more work in a shorter amount of time thanks to asynchronous programming.

Asynchronous programming is frequently associated with parallelization, which is the practice of carrying out separate activities in parallel utilizing, you guessed it, asynchronous programming.

What is typically processed sequentially can be divided into smaller units that can run independently and concurrently using parallelization. In addition to methods and capabilities, parallelization has to do with how systems and software are made.

Applying parallelization concepts can enable you to get results much more quickly and efficiently.

Code Sample

using System;

namespace CSharpSynchronousExample
{
    // Async Await example
    class Program
    {
        public static async Task Main(string[]args)
        {
            Console.WriteLine("OUTPUT");

            await GetStudentName();
            await GetStudentNumber();
            Console.WriteLine("All async Tasks run successfully");
            Console.ReadLine();
        }

        private static async Task GetStudentName()
        {
            await Task.Delay(1000);
            Console.WriteLine("Student Name is Samuel");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "1";

            Thread currentThread = Thread.CurrentThread;
            Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }

        private static async Task GetStudentNumber()
        {
            await Task.Delay(1000);
            Console.WriteLine("Student Number is 12");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "2";

        Thread currentThread = Thread.CurrentThread;
        Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }
    }
}

Output

AS.png

Is Asynchronous Programming Better Than Synchronous Programming?

Simply put: No. Although there are many advantages, not all processes should execute asynchronously and in parallel. Let's examine when asynchronous programming is appropriate and when sticking with synchronous execution is the best choice.

When does a software not require asynchronous processing?

Asynchronous programming is only appropriate when dealing with independent tasks.

  • A micro-service is constantly barraged with requests and has very little CPU-bound operations. Async is therefore not required.

  • A database that cannot scale is being called by a service. Making the code async won't assist in this situation because the database will become a bottleneck.

  • If an application contains I/O-bound task that is extremely sluggish but does not get a lot of requests, it does not need to scale.

  • a monolithic application that performs numerous CPU-bound computations, each of which is dependent on the computation before it.

Thanks for reading...

Happy Coding!

More from this blog

Building Reliable Systems

93 posts

Insights on Infrastructure, DevOps, SRE, and building reliable systems at scale.