How To create a new Thread & Start it/Stop it
Add Reference : using System.Threading;
Create Thread : Thread thread = new Thread(
new ThreadStart(parametereless method));
Start Thread : mythread.Start();
Add Reference : using System.Threading;
Create Thread : Thread thread = new Thread(
new ThreadStart(parametereless method));
Start Thread : mythread.Start();
- Code:
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Class1
{
static void PrintHelloFromThreadName()
{
Console.WriteLine("Hello, from thread {0}",
Thread.CurrentThread.Name); // {0}
}
public void ThreadStart()
{
PrintHelloFromThreadName();
}
static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main thread";
Class1 obj = new Class1();
Thread thread = new Thread(
new ThreadStart(obj.ThreadStart));
thread.Name = "Forked thread";
thread.Start();
PrintHelloFromThreadName();
}
}
}