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
situs toto pucuk4d agb99 toto slot toto togel indobet situs toto toto slot toto slot toto slot toto slot depo 10k 8kuda4d judi bola toto situs toto toto toto situs toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto UGSLOT slot gacor ilmutoto https://foundvinylrecords.com/ depo 5k slot gacor panen100 semibola MySlot188 monk4d popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot slot gacor kaskustoto situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ mataramtoto https://shopjoliboutique.com/ apo388 slot 4d RP888 toto toto toto toto toto slot gacor toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto slot gacor sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d link slot sumbartoto Login bandar toto mataramtoto popotogel login slot gacor nobu99 situs toto logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo Slot Malaysia bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login 33ag rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot situs slot slot ilmutoto toto toto slot SLOT GACOR slot gacor slot bento11 link slot gacor
situs toto pucuk4d agb99 toto slot toto togel indobet situs toto toto slot toto slot toto slot toto slot depo 10k 8kuda4d judi bola toto situs toto toto toto situs toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto UGSLOT slot gacor ilmutoto https://foundvinylrecords.com/ depo 5k slot gacor panen100 semibola MySlot188 monk4d popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot slot gacor kaskustoto situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ mataramtoto https://shopjoliboutique.com/ apo388 slot 4d RP888 toto toto toto toto toto slot gacor toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto slot gacor sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d link slot sumbartoto Login bandar toto mataramtoto popotogel login slot gacor nobu99 situs toto logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo Slot Malaysia bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login 33ag rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot situs slot slot ilmutoto toto toto slot SLOT GACOR slot gacor slot bento11 link slot gacor