If you've ever wondered how to get more performance out of your programs, without using some specific language tricks, you've probably heard of multithreading.
I'll try and explain in a really basic way how it works, and then show you when it can apply, how NOT to do it, and some final tips on how to use it.

What is multithreading

As you might know, your CPU has multiple cores. You can consider that a CPU core can run an application on its own. The goal of multithreading is to use multiple cores at the same time, in order to run faster.

An application (process) can use multiple threads. A thread is where the code of your program is ran. It's the role of the OS to manage threads and split them amongst all of the availble cores.

In order to use multiple cores to run faster, an application must use multiple threads, hence the name multi-threading.

Now that you understand a little bit more about multithreading, let's talk about what you should NOT do. (If you still have some problems understanding this, please go check wikipedia.)

What not to do

As with any other kind of optimisation, you should consider a few things first.

Is my application already functional? If you really think that your application can work on it's own, then you can consider adding multithreading as a feature. IT IS NOT A CORE FUNCTIONALITY.

Another guestion you can ask yourself is: Will my application really benefit from multithreading? Most basic applications don't, and it will just be a painful task to try and add multithreading to it.

You've decided to add multithreading to your application. Great, but if you use a language, like C#, which asks you to lock resources accessed by multiple threads, DO NOT OUTPUT YOUR RESULTS IN A SHARED VARIABLE. This will cause each thread to wait for all others to stop saving the result. Instead, just return multiple results that you can combine afterwards.

Some tips

Think about how to divide your workload before starting to code: Think about how cinebench r15, or my program Mass Resizer are doing it.

Cinebench has a huge number of computations to do, so the total workload is divided in about 200 "tasks" to do, and each thread will work on a new task as their is finished.

MassResizer is supposed to handle about 200 files, so I simply divided the number of files by the number of threads, which is much more simpler to do than Cinebench's way, but it also means that when approaching the end, fewer threads are working, as some may have finished their part faster.

Anyway, I hope this helped you in some way, and I hope you will have less troubles realising your next multithreaded application.

Categories Programming


Context (sort of)

Recently I've been trying to improve my personnal project Windmenu, and one of the problems I found, was that while crawling through the Start Menu to retreive all apllications, ModernUI applications were not found.

I soon realised that, the usual directory %appdata%\Microsoft\Windows\Start Menu did in fact not contain those shortcuts.

So where to find them?

A few websites point to shell:::{4234d49b-0245-4df3-B780-3893943456e1}, and that is true, but you sadly cannot acess that from C#.

The truth is: I don't know "where" to find them, but I know "how". This is how

How I did it

Listing the applications

To be honest I am not proud of this method, as it relies on the registry, which is most of the time not a good idea.

If you go to HKEY_CURRENT_USER\Software\Classes in regedit, you will see all the file/url extensions, but also what we are looking for.

Here, are a few keys, named using the following pattern: AppX[a-z0-9]*. Those are the applications. Keep them.

These are the keys we are looking for, as they contain in the Application subkey the value of AppUserModelID.

AppUserModelID is what is needed to later, start a modernUI application.

Getting the name of the application

Once again, not proud of this. I am sur there is a better way to do it.

What I did was retrieve the "name" from the AppUserModelID with this regex (.+\.)+([a-zA-Z]*)_.*!.* and then "prettyfied" it, adding spaces where needed.

The registry key contains a value named ApplicationName, but I could not find a way to use this to retreive the local dependant name.

Starting the application

Here I didn't do much, as user sankar over at StackOverflow already explained the whole process of starting a modernUI application using it's AppUserModelID.

This involves some com interfacing, and I am not going to go any more deeper than this about the subject, since you can just check out my code (here) to see how I did it.


There must be a better way. I don't know why Microsoft made it so hard to start a modernUI application from a regular application, but they did it.

Should you do it the way I did it? No. It's bad, and I realy hope I will find a way to do it better.

Does it work? Yes, as long as Microsft does not decide to change the registry keys this is based on.

Anyway, I hope I will be able to find a new way to do this, which I will post here.

Categories C#


Such first post
Very much to come
Many great

Categories Random