Retrace will reach End of Life on March 31, 2027. Click here to learn more.

Asynchronous Programming Is Easier Than You Think

By: mwatson
  |  February 19, 2017
Asynchronous Programming Is Easier Than You Think

What is asynchronous programming?

To understand that let us take a look at how applications typically run. Typically when you are writing a program it executes one method at a time. The CPU executes a method then once it finishes moves to the next one. This is known as synchronous programming.

I’m a big fan of understanding the taxonomy and etymology of words. I feel like it gives a lot of initial insight. So let’s look at the root word:

> syn·chro·nize:
cause to occur or operate at the same time or rate.

So the idea with synchronous programming is that things all happen at the same rate. One thing starts, and then the next thing starts afterward, in order, at the same rate.

Asynchronous programming means being able to do things out of order, I.E. not synchronized.

How do we do things out of order?

The basic building block of asynchronous programming is the Thread. A thread has 2 meanings. It is often referred to with CPU’s as for how many individual sets of synchronous actions the processor can to at the same time (in parallel.) It is also a logical unit that the operating system uses to manage tasks that it is sending to the CPU. So the processor its self may only have a few threads it can run in parallel, but the OS may have thousands of threads open at any given time depending on how many applications are running and what each of those applications is doing.

It’s like a bank. There are more customers than clerks, so everyone gets in a line and as a clerk becomes available they help the customer. Likewise, individual threads are either making deposits or making withdrawals. When they are “depositing” they have an action they need to preform then leave and let the bank do its thing. When they make a withdrawal the customer waits for a returned sum of money.

Making a deposit

static void Main(string[] args)
{
	Thread bankThread = new Thread(() => {
		Thread.Sleep(500);
		Console.WriteLine("Bank: Money has is now available in the account.");
	}); 

	Console.WriteLine("Customer: I would like to deposit");
	Console.WriteLine("Clerk: No problem");

	bankThread.Start();

	Console.WriteLine("Customer: Thank you");

	Console.ReadLine();
}

This will generate the following output:

Customer: I would like to deposit
Clerk: No problem
Customer: Thank you
Bank: Money is now available in the account.

Making a withdrawal is a little different because we have to have some way of giving the customer back something from the thread. We do this with a callback.

Making a withdrawal

static void Main(string[] args)
{
	Thread withdrawThread = new Thread(x => {
	Thread.Sleep(500);
	decimal money = 20;
	Console.WriteLine("Clerk: Here is {0:C2}", money);
	var callback = (x as Action);
	callback(money);
	});

	Action customerCallback = money => { Console.WriteLine("Customer: Thank you for the {0:C2}", money); };

	Console.WriteLine("Customer: Hi, I'd like twenty dollars");
	Console.WriteLine("Clerk: No problem, let me get that for you");

	withdrawThread.Start(customerCallback);

	Console.WriteLine("Customer: twiddles thumbs");

	Console.ReadLine();
}

This will generate the following output:

Customer: Hi, I’d like twenty dollars
Clerk: No problem, let me get that for you
Customer: twiddles thumbs
Clerk: Here is $20.00
Customer: Thank you for the $20.00

So I hope this helps you find the thought of asynchronous programming a lot less scary. I’ll be writing more articles on the subject and how we leverage the Task Parallel Library, how all of this relates to async and await, and how we handle and log exceptions.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
indobet toto slot toto toto bobatoto daftar logototo patentoto toto bayitoto popotogel prize hongkong malam ini joker11 lingkartoto depo 5k https://nolimithoki.dev/ dinasti33 slot slot jepang toto toto flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel mpo slot situs toto slot situs slot bobatoto kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d bento11 link slot gacor toto toto slot toto situs slot situs slot situs slot https://www.asupan188.dev/ slot gacor gampang menang toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot slot gacor pucuk4d PASCOL4D situs slot link slot slotvip situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 situs judi bola POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat link slot slot gacor situs slot slot gacor slot gacor situs slot link slot gacor Slot88 Slot88 slot gacor situs toto toto slot pulsa slot777 link slot gacor slot gacor slot gacor slot gacor slot gacor Slot88 situs slot slot gacor Slot88 slot gacor slot terbaru slot gacor situs slot slot gacor situs slot Situs Slot POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya StreamEast slot resmi slot slot resmi slot bayitoto link gacor slot gacor kari4d slot gacor slot gacor slot gacor pascol4d kari4d kari4d bragatoto slot gacor slot game pucuk4d rokokbet rokokbet kari4d pascol4d LOGIN PASCOL4D pucuk4d judi bola slot depo 10k mpo slot StreamEast AMANAHTOTO jutatoto PARLAY slot88 slot depo 5k hoki99 pascol4d pkv games roma77 Slot poker slot situs slot PREMANTOTO AMANAHTOTO AMANAHTOTO AMANAHTOTO dominoqq vespa36 kari4d vespa36 leon188 leon188 vespa36 gacor mpo login TOTO SLOT hoki99 KENANGAN889 KENANGAN889 KENANGAN889 KARI4D parlay parlay parlay KENANGAN889 KARI4D PASCOL4D PUCUK4D vespa36 batak5d KARI4D monk4d pucuk4d patentoto patentoto vespa36 vespa36 okejp QQ288 roma77 jutatoto jpmania
indobet toto slot toto toto bobatoto daftar logototo patentoto toto bayitoto popotogel prize hongkong malam ini joker11 lingkartoto depo 5k https://nolimithoki.dev/ dinasti33 slot slot jepang toto toto flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel mpo slot situs toto slot situs slot bobatoto kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d bento11 link slot gacor toto toto slot toto situs slot situs slot situs slot https://www.asupan188.dev/ slot gacor gampang menang toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot slot gacor pucuk4d PASCOL4D situs slot link slot slotvip situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 situs judi bola POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat link slot slot gacor situs slot slot gacor slot gacor situs slot link slot gacor Slot88 Slot88 slot gacor situs toto toto slot pulsa slot777 link slot gacor slot gacor slot gacor slot gacor slot gacor Slot88 situs slot slot gacor Slot88 slot gacor slot terbaru slot gacor situs slot slot gacor situs slot Situs Slot POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya StreamEast slot resmi slot slot resmi slot bayitoto link gacor slot gacor kari4d slot gacor slot gacor slot gacor pascol4d kari4d kari4d bragatoto slot gacor slot game pucuk4d rokokbet rokokbet kari4d pascol4d LOGIN PASCOL4D pucuk4d judi bola slot depo 10k mpo slot StreamEast AMANAHTOTO jutatoto PARLAY slot88 slot depo 5k hoki99 pascol4d pkv games roma77 Slot poker slot situs slot PREMANTOTO AMANAHTOTO AMANAHTOTO AMANAHTOTO dominoqq vespa36 kari4d vespa36 leon188 leon188 vespa36 gacor mpo login TOTO SLOT hoki99 KENANGAN889 KENANGAN889 KENANGAN889 KARI4D parlay parlay parlay KENANGAN889 KARI4D PASCOL4D PUCUK4D vespa36 batak5d KARI4D monk4d pucuk4d patentoto patentoto vespa36 vespa36 okejp QQ288 roma77 jutatoto jpmania