Creating threads is easy to do in C#. So, the real question is this: Why do you want to go to the trouble of using multiple threads? The answer lies in the following statement.
Any time you need to do concurrent operations, the true solution is threading. Yes, it has some caveats, but the end results makes threading worth the effort.
Another great feature of multithreading in C# is that it does not require you to rely only on static thread procedures (as in the following example). You can start new threads using a static method or instance method. You can actually use an instance-based class, and pass the whole class to be worked on by another thread by calling a method on another instance-based class in the ThreadStart delegate.
| Note |
When a thread is created in managed code, it defaults to being a foreground thread. When a thread is created in unmanaged code and transitions into managed code, it is considered a background thread. This is important because in a managed application when there are no foreground threads, the process will end. |
using System; using System.Threading; namespace Client.Chapter_15___Threading { class CreatingThreads { static void Main(string[] args) { Thread MyNewThread = new Thread(new ThreadStart(ThreadProc)); MyNewThread.Start(); MyNewThread.Join(); } protected static void ThreadProc() { for (int i = 0; i > 100; i++) { Console.WriteLine(i); } } } }