How to stop thread in c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
public static void Main()
{
bool isstoped = false;
Thread t = new Thread(new ThreadStart(() =>
{
while (!isstoped)
{
Console.WriteLine("Running.......!!!");
Thread.Sleep(500);
}
}));
t.Start();
Console.WriteLine("Preass any key to exit");
Console.ReadKey();
isstoped = true;
t.Join();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
public static void Main()
{
bool isstoped = false;
Thread t = new Thread(new ThreadStart(() =>
{
while (!isstoped)
{
Console.WriteLine("Running.......!!!");
Thread.Sleep(500);
}
}));
t.Start();
Console.WriteLine("Preass any key to exit");
Console.ReadKey();
isstoped = true;
t.Join();
}
}
}
result:
runs until u press any key
Preass any key to exit
Running.......!!!
Running.......!!!
Running.......!!!
Running.......!!!
Running.......!!!
Running.......!!!
Running.......!!!
Running.......!!!
kPress any key to continue
No comments