Certainly! Here’s an example of using threading in C# with a static method:
using System; using System.Threading; public class Program { public static void Main() { // Create a new thread and start it Thread t = new Thread(StaticMethod); t.Start(); // Main thread continues execution for (int i = 0; i < 5; i++) { Console.WriteLine("Main thread: " + i); Thread.Sleep(1000); } } public static void StaticMethod() { // Code to be executed by the new thread for (int i = 0; i < 5; i++) { Console.WriteLine("New thread: " + i); Thread.Sleep(1000); } } }
In this example, we define a static method called StaticMethod
which will be executed by a separate thread. The Main
method creates a new thread using Thread
class and passes StaticMethod
as the target method for the new thread. The Start
method is then called to begin the execution of the new thread.
Meanwhile, the main thread continues execution and enters a loop that prints a message and sleeps for 1 second. This loop runs concurrently with the execution of the new thread.
When you run this program, you’ll see output from both the main thread and the new thread intermixed, as they execute concurrently. The output might look something like this:
Main thread: 0 New thread: 0 New thread: 1 Main thread: 1 New thread: 2 Main thread: 2 New thread: 3 Main thread: 3 New thread: 4 Main thread: 4
Note that the exact order of execution may vary each time you run the program, as it depends on the scheduler and the timing of the thread operations.
C# Threading Example: non-static method
Certainly! Here’s an example of using threading in C# with a non-static method:
using System; using System.Threading; public class Program { public static void Main() { // Create an instance of the MyClass MyClass myClass = new MyClass(); // Create a new thread and start it, passing the non-static method Thread t = new Thread(myClass.NonStaticMethod); t.Start(); // Main thread continues execution for (int i = 0; i < 5; i++) { Console.WriteLine("Main thread: " + i); Thread.Sleep(1000); } } } public class MyClass { public void NonStaticMethod() { // Code to be executed by the new thread for (int i = 0; i < 5; i++) { Console.WriteLine("New thread: " + i); Thread.Sleep(1000); } } }
In this example, we define a class called MyClass
with a non-static method called NonStaticMethod
. The Main
method creates an instance of MyClass
and then creates a new thread using Thread
class, passing the NonStaticMethod
as the target method for the new thread. The Start
method is then called to begin the execution of the new thread.
Similar to the previous example, the main thread continues execution and enters a loop that prints a message and sleeps for 1 second. This loop runs concurrently with the execution of the new thread.
When you run this program, you’ll see output from both the main thread and the new thread intermixed, as they execute concurrently. The output might look something like this:
Main thread: 0 New thread: 0 New thread: 1 Main thread: 1 New thread: 2 Main thread: 2 New thread: 3 Main thread: 3 New thread: 4 Main thread: 4
Again, note that the exact order of execution may vary each time you run the program.
C# Threading Example: performing different tasks on each thread
Certainly! Here’s an example of using threading in C# to perform different tasks on each thread:
using System; using System.Threading; public class Program { public static void Main() { // Create a new thread and start it, passing a task Thread t1 = new Thread(() => PerformTask("Task 1")); t1.Start(); // Create another new thread and start it, passing a different task Thread t2 = new Thread(() => PerformTask("Task 2")); t2.Start(); // Main thread continues execution for (int i = 0; i < 5; i++) { Console.WriteLine("Main thread: " + i); Thread.Sleep(1000); } } public static void PerformTask(string taskName) { // Code to be executed by the new thread for (int i = 0; i < 5; i++) { Console.WriteLine(taskName + ": " + i); Thread.Sleep(1000); } } }
In this example, we create two new threads, t1
and t2
, and pass lambda expressions that represent the tasks we want each thread to perform. Each lambda expression calls the PerformTask
method with a different task name.
When the threads start, they execute their respective tasks concurrently with the main thread. The tasks are identified by their task names, which are passed as parameters to the PerformTask
method.
The main thread also continues execution and enters a loop that prints a message and sleeps for 1 second.
When you run this program, you’ll see output from all three threads intermixed, as they execute concurrently. The output might look something like this:
Main thread: 0 Task 1: 0 Task 2: 0 Task 1: 1 Main thread: 1 Task 2: 1 Task 1: 2 Main thread: 2 Task 2: 2 Task 1: 3 Main thread: 3 Task 2: 3 Task 1: 4 Main thread: 4 Task 2: 4
As with the previous examples, the exact order of execution may vary each time you run the program.