Search code examples
c#multithreading.net-corewindows-servicesbackground-service

Do background threads run slower than other threads?


I have a C# console application running as a Windows Service. It implements BackgroundService, which is the recommended way to implement Windows services.

In addition, I'm using Hangfire to initiate my tasks.

I've noticed that a longer running tasks takes nearly 11 minutes, while the same task run on my local machine (not as a Windows service, not using BackgroundService, and in debug mode) takes less than 6 minutes.

Am I to assume that background threads generally run slower? Or are there likely other issues going on here?

The code itself performs a bunch of calculations on data from an SQL Server database and updates the data.


Solution

  • Threads run at their given priority, you can give a particular task a higher priority, which means that it will be executed before any threads of a lower priority. A background thread can have a higher priority than other threads, and a UI thread can have a lower priority.

    In practice, this doesn't really make much difference, as you probably have a multi-thread/core processor.

    If you are accessing a database, your thread/task is probably waiting for the database server to reply (i/o bound operations). Unless you are doing really complicated calculations (CPU bound operations) the thread is probably waiting for something external to happen, and that is outside your control.