C# – Some simple example for threads

The idea here is to show a little bit about parallel programming and the way I understand it – as far as I am mainly a VBA developer, I do not understand it, because Microsoft did not think that it can be useful in VBA. 🙂

Anyhow, the idea of the parallel programming (or multiple thread programming) is that there can be tasks carried out in the same time, and not like in VBA (or PHP and the other scripting languages) just one task. It is really useful, if you know how to use it. Its like having multiple tasks in a program, which do not wait for each other, but carry out simultaneously. Pretty much that is it.

In C#, there is a .NET built-in library, which you can use to do it – System.Threading. In the current example I create 3 Threads and I give to two of them a repetitive task to write on the console some string. Then I make the Thread sleep a bit and then I abort the threads. The result is seen on the screenshot.

Here comes the code:

And the Writer class:

Yup that’s all.

The code is in GitHub as well.

Tagged with: ,