Thread count problem in C# multithreading

Interview question: How many total threads are there in below C# multithreading program?   static void Main(string[] args) { Thread T1 = new Thread(foo); Thread T2 = new Thread(foo); T1.Start(); T2.Start(); } static void foo() { // some operation }   Answer: Total 3 threads are there in the above C# program. We can see … Read more Thread count problem in C# multithreading

Can one process run multiple threads in C#

Answer: Yes, in a single process we execute multiple threads in C# multithreading program. For example in below program, main () is a process that can create multiple child threads. We have created and run two threads t1 and t2.   using System; using System.Threading; class ThreadDemo { private static Object _lock = new Object(); … Read more Can one process run multiple threads in C#

How to pass parameter to thread in C#

Pass parameter to thread in C# interview question – If we want to pass parameter to thread, How would you do that? We need to explain by a thread program example. Answer: Argument / parameter to a thread function can be passed in multiple ways and here is the 2 ways to pass parameter to … Read more How to pass parameter to thread in C#

C# Lock Vs Monitor – Why Use Monitor instead of Lock

Answer: Lock Vs Monitor in C# multithreading: Difference between monitor and lock in C# is that lock internally wraps the Enter and Exit methods in a try…finally block with exception handling. Whereas for Monitor class in C#, we use try and finally block explicitly to release lock properly. Lock = Monitor + try finally. Below … Read more C# Lock Vs Monitor – Why Use Monitor instead of Lock

What is C# monitor class? Explain monitor pulse wait by example

Answer includes C# monitor class with program example in threaded program and its pulse wait method. Monitor class in C# multi-threaded program is used to synchronize the shared resources i.e. file IO, shared data and memory etc. among threads. Monitor class is available in namespace System.Threading. Besides synchronization mechanism, Monitor class also provides wait (), … Read more What is C# monitor class? Explain monitor pulse wait by example

C# thread priority – detail whatever you know

Every C# thread has a priority. Thread priorities in C# are available in ThreadPriority class from namespace System.Threading; Below C# thread priority can be assigned to a thread. Highest AboveNormal Normal BelowNormal Lowest By default “Normal” thread priority is assigned to a thread in C#. Thread scheduler of operating system schedules all threads according to … Read more C# thread priority – detail whatever you know

Even and Odd number sequence with 2 threads in C# Program

C# multithreading Interview Question: Using two threads print even and odd number in sequence. One thread prints even numbers and another thread prints odd numbers. Thread T1 : 0,2,4,6… Thread T2 :1,3,5,7… Output: 0, 1,2,3,4,5,6,7… How to create Thread in C# Program? Answer: To solve this problem let’s use signalling mechanism using  wait () and … Read more Even and Odd number sequence with 2 threads in C# Program

Satisfy need of thread synchronization in C# multithreading

For this C# thread synchronization interview question, we should be answering need of thread synchronization in C# with program example using with and without synchronization. Answer: When sharing of resource i.e. file IO and shared memory etc. among multiple threads comes into picture we need thread synchronization. Let’s take a thread program example in which … Read more Satisfy need of thread synchronization in C# multithreading

How do you create thread in C# Multithreading

Answer: To create thread in c# multitheading, we need to use the namespace System.Threading .This name space is used to link the thread assembly file create thread in C# programs. “using System.Threading” We will be using Thread class to create a thread. First of all, we have to create thread object then pass a user … Read more How do you create thread in C# Multithreading