This is simple example to demonstrate the making a thread and starting it in C#. We have took a example to declare a start thread and pass this startthread to a thread constructor while declaring the thread.
Once you get the thread object, directly call
t1.Start();
to start the thread.
Now look at the thread execution.
- Before Thread Execution Starts
- After Thread Execution Starts
- Inside Thread Method
The “Inside Thread Method” is get called in last, the start() method just start the thread and execute next statement of the block.
Example to Create and Start the Thread in C# Example:
using System;
using System.Text;
using System.Threading;
namespace Console_App
{
public class Class1
{
public static void function()
{
Console.WriteLine("Inside Thread Method");
}
}
public class Class2
{
public static void Main()
{
ThreadStart thread1 = new ThreadStart(Class1.function);
Thread t1 = new Thread(thread1);
Console.WriteLine("Before Thread Execution Starts");
t1.Start();
Console.WriteLine("After Thread Execution Starts");
Console.ReadLine();
}
}
}
0 comments:
Post a Comment