All pastes #84982 Raw Edit

Poopy example...

public text v1 · immutable
·published 2006-07-11 15:16 UTC
rendered paste body
using System;
using System.Threading;

namespace ConsoleApplication1
{
	class Program
	{
		static object _lock = new object();
		static bool _KeepRunning = true;

		static bool KeepRunning
		{
			get
			{
				lock (_lock)
				{
					return _KeepRunning;
				}
			}
			set
			{
				lock (_lock)
				{
					_KeepRunning = value;
				}
			}
		}

		static void KillBackGroundThread()
		{
			KeepRunning = false;
		}

		static void Main(string[] args)
		{
			Thread bgThread = new Thread(new ThreadStart(MyThreadMethod));
			bgThread.IsBackground = true;
			bgThread.Name = "Background";
			bgThread.Start();

			Thread.Sleep(10000);

			KillBackGroundThread();
		}

		static void MyThreadMethod()
		{
			while (KeepRunning)
			{
				Console.WriteLine("Running");
				Thread.Sleep(1000);
			}
		}
	}
}