C# abort a thread

Hi

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(() =>
           {
                CheckProjects();
            });
th.Start();

Open in new window


I tried running

th.Abort();

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.

Thanks
Fixitben
LVL 7
fixitbenAsked:
Who is Participating?
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.
0

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
it_saigeDeveloperCommented:
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();
				watch.Start();
				while (!token.IsCancellationRequested) { ;}
				watch.Stop();
				return watch.ElapsedMilliseconds;
			}, token);
			task.Start();
			Console.WriteLine("Press any key to stop the task");
			Console.ReadLine();
			source.Cancel();
			Console.WriteLine("Task ran for {0} milliseconds...", task.Result);
			Console.ReadLine();
		}
	}
}

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-
0
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.
0
Nadav SolomonCommented:
Glad I could help, thanks for the feedback.
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.