Tidbit Tuesday – Easy Multi-Threading

Multi-Threading is the ability for an application to perform multiple tasks at the same time. By default an application has only one thread, the Primary Thread; then Auxiliary or Worker Threads are created to run code in Parallel with the primary thread. A common use for Multithreading is to have the UI (User Interface) run on the primary thread and have the rest of the code run on one or more worker threads.  This keeps the application responsive to the user while all the details are calculated in the background.

Threading however introduces some new issues related to variable usage and you must be careful that you do not have 2 threads  attempting to change the same variables at the same time. I’m not going to go into that today as it is well outside the scope of this tidbit; just be aware that it can cause multiple issues including crashes.

Today we are going to talk about Asynchronous Programming (Async); this is a super easy way to write multithreaded applications without having to learn all the ins & outs of Multithreading. Async requires .Net 4.5, so if you are running an earlier version of .Net, Monodevelop or Unity these features will probably not work.

Async is best used for activities that might block your application from running, or in reverse maintain basic responsiveness when the application is loading.  A simple example would be to show loading text while your application is loading:

So what have we got above?  This is a simple console application that simulates the loading of an application and updates some onscreen text showing how far along we are.  The first part (in the Main method) is pretty standard stuff. It just updates the loading variable.  You can change the Sleep number to adjust how fast the application pretends to load.

The second part is where the Async stuff happens. The private & static parts are just like any other method. This method could be public & does not need to be static.  Async methods must return void or Task<T>.  The only important part is that we add in async.  This is the first part of declaring an Async method, the second part is await. This specific line is telling it to wait for a short delay before updating the text.  There are however other await options related to Task.  If this method was missing the await expression, or the Delay was set to 0 it would run just like a Synchronous thread and the application would never load. I highly recommend copying the above into your IDE and messing around with it; try changing the values in Sleep & Delay and try removing the await statement to see how the application changes.

If you were creating a game, and needed something to happen after a set amount of time you could also do something like this:

Async methods can also return values via Task<T>, for example if you wanted to return a string you could do something like this:

In the above we run an Async method and get the results from that method. If you notice we check to see if the Task is done, if not we update the waiting text.  It is important that you check to make sure you Async method is done running before you try to get the results (unless you want to wait).  When you try to get the results from an Async method it halts the application at that point until the method is done running and the result is ready.  While this can be desirable behavior in some situations it can defeat the purpose of Async.

That should give you enough to get started. There is a TON of information out there detailing the proper usage of Asynchronous Programming and a lot more complicated examples depending on what you want to get out of it. As always questions and constructive criticism are encouraged in the comments!

Posted in Beyond Mindless, Code Snippets, Programming and tagged , , , , , , , , .