So what are the real benefits to consider learning how to leverage multiple threads (note that I take for granted you know the difference between a process and a thread) of execution within an application. Well, there are a great amount of benefits to do that, such as:
- The opportunity to scale by parallelizing CPU-bound operations. This has to do with the overall idea of running an application specifically designed to deal with multi-threading in a single-core computer or one that does not have more than only one processor.
- Maintain an user interface that is really responsive to users thanks to asynchronous execution techniques.
A dummy demonstration to support the latest point that I mentioned involves to develop a Windows Form based application which fails to respond to the user. Note that this app is called in this way (MDPA4W) because its poor performance reminded me of My Dark Past at 4 Winds (kidding errr kidding…)
As you can see there’s a single button on it. I’m going to use the System.Threading libray and the method Sleep to make it stall for about 6 seconds. Thus this app should be blocked for about 6,000 milliseconds when the handler associated to the button works.
The idea behind this app is pretty straight fordward. If I move one program back and forth over the form I’ve just created, the form will be automatically repainted. But the thing is if I click the button, the app will be blocked about 6 seconds due to the given call to the Sleep method. So this obviously cannot be able to repaint the main app display here (not drawing attention to the notifications that Windows is sending!)
So to deal with it, the approach can be to use long running operations in the context of some other thread that is not bothered by being blocked.