Languages
[Edit]
EN

C#/.NET - Foreground & Background Threads

5 points
Created by:
martineau
1350

In this article, we would like to show how to create foreground and background threads in C# / .NET.

Foreground vs Background thread:

  1. Foreground thread is threads that is still executed after main thread stopped working
    (that threads prevent a process from terminating).
  2. Background thread is threads that ends execution when main thread stops execution.

Quick solution:

 

1. Foreground thread example

Edit

Output:

Summary:

My foreground thread ended execution after main thread ended.

 

2. Background thread example

Edit

Output:

Summary:

My foreground thread stopping... was never printed because bacground thread ended with main thread.

 

References

Edit
  1. Thread.IsBackground Property - Microsoft Docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - Thread Class

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join