C# abort a thread


I am new to working with threads. I have something that runs under a thread that just scans for projects and makes sure the folder path exists.

I start it using
Thread th =  new Thread(() =>

Open in new window

I tried running


Open in new window

But that doesn't stop the thread from running.

Is there an easy way to kill that thread? I have a cancel button I would like to use to stop it if needed for some reason mid scan.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nadav SolomonCommented:
You need to create some global variable for example bool exitThread and check that variable all the time in your thread, if that variable changes to lets say true you will exit the thread from within the thread itself, this way you can release locks or what ever this thread is holding.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Which .NET version are you using?  If you are using .NET 4 or above, it is more preferable to use Tasks as opposed to Threads directly.  The reason I mention this is because Tasks can be configured to include a cancellation token.

Proof of concept -
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace EE_Q28761679
	class Program
		static void Main(string[] args)
			CancellationTokenSource source = new CancellationTokenSource();
			CancellationToken token = source.Token;
			Task<long> task = new Task<long>(() => 
				Stopwatch watch = new Stopwatch();
				while (!token.IsCancellationRequested) { ;}
				return watch.ElapsedMilliseconds;
			}, token);
			Console.WriteLine("Press any key to stop the task");
			Console.WriteLine("Task ran for {0} milliseconds...", task.Result);

Open in new window

Which produces the following output -Initial run.  The task runs in the background while the main thread waits for a key to be pressed.Key pressed.  The stopwatch result is returned.-saige-
fixitbenAuthor Commented:
Thanks. This worked great. It took me a little bit to figure out where in the code to place it, but then it works like a charm.
Nadav SolomonCommented:
Glad I could help, thanks for the feedback.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.