A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to create a thread
Q:

C# program to create a thread

0

C# program to create a thread

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to create a thread in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

/*
 * Program to create a thread in C#
 */
using System;
using System.Threading;

class ThreadEx
{
    public void MyThreadFun()
    {
        int i = 0;
        for (i = 1; i<=4; i++)
        {
            Console.WriteLine("Thread Executed");
        }
    }
}
class Program
{
    public static void Main()
    {
        ThreadEx T = new ThreadEx();
        Thread t1 = new Thread(new ThreadStart(T.MyThreadFun));
        t1.Start();
    }
}

Output:

Thread Executed
Thread Executed
Thread Executed
Thread Executed

Explanation:

In the above program, we imported System.Threading namespace to create a thread. Here we created a class ThreadEx that a method MyThreadFun() to implement thread. We also created a Program class that contains the Main() method.

In the Main() method we created object T of class ThreadEx.

Thread t1 = new Thread(new ThreadStart(T.MyThreadFun));

In the above statement we passed the method to delegate ThreadStart and then pass delegate ThreadStart as a parameter in the constructor of Thread class, and then finally called Start() method to execute the thread.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now