views:

662

answers:

7

I need to have a thread signal another if the user wishes to interrupt execution, however I'm unsure about how to implement the signaling/signal-checking mechanism. I wouldn't like to have a singleton in my project (like a global bool), but is there an alternative?

In this thread people suggest proper structures for that in C++, but I don't know about anything similar in .NET. Could somebody please shed some light?

A: 

Look at the System.Runtime.Remoting namespace.

Joel Coehoorn
... And then run away screaming.
Will
... and then wonder what it has to do with your question.
Matt Howells
+4  A: 

A bit vague (short of time), but look into ManualResetEvent and AutoResetEvent. You also might want to look up Monitor and lock keyword.

Statement
Problem with these is that someone must sit waiting for them to be set, or must poll for them to be set. The "main" thread cannot sit, and i don't want a busy-loop, battery wasting, energy-consuming, poll.
Ian Boyd
+7  A: 

Try out BackgroundWorker. It supports progress updates and cancellation of a running task.

If you want one thread to wait until another thread has finished doing its thing, then Monitor.Wait and Monitor.Pulse are good, as is ManualResetEvent. However, these are not really of any use for cancelling a running task.

If you want to write your own cancellation code, you could just have a field somewhere which both threads have access to. Mark it volatile, e.g.:

private volatile bool cancelling;

Have the main thread set it to true, and have the worker thread check it periodically and set it to false when it has finished.

This is not really comparable to having a 'global variable', as you can still limit the scope of the semaphore variable to be private to a class.

Matt Howells
I'd prefer to go with framework solutions. Doesn't the shared var smell like a singleton? Thanks for the answer!
André Neves
BackgroundWorker is part of the framework (2.0, I believe). I agree a shared variable is a smelly solution to the issue.
Will
Oh, no, I am actually tending to go with the BW approach. The un-frameworkey reference was towards implementing my own private hell :)
André Neves
+3  A: 

Look into Monitor.Wait and Monitor.Pulse. Here is an excellent article on Threading in .Net (very readable): http://www.albahari.com/threading/part4.aspx

torial
Monitor.Wait and Monitor.Pulse aren't really applicable to the cancellation scenario.
Matt Howells
Problem with these is that someone must sit waiting for them to be set, or must poll for them to be set. The "main" thread cannot sit, and i don't want a busy-loop, battery wasting, energy-consuming, poll.
Ian Boyd
+1  A: 

A simple solution, like a synchronized static boolean, should be all you need as opposed to a framework-based solution which copuld be overkill for your scenario. In case you still want a framework, have a look at the parallel extensions to .NET for ideas.

Mark Cidade
Actually, adapting my project to take advantage of the BackgroundWorker would probably bring more benefits, since it has some useful abstractions. But if it was something really simple, I wouldn't think twice before doing a volatile bool!
André Neves
But how is the "main" thread signalled that something has happened? It doesn't know that a boolean variable has been set.
Ian Boyd
The main thread would have to poll the boolean field, typically in a loop.
Mark Cidade
A: 

It depends on what kind of synchronization you need. If you want to be able to run thread in a loop until some kind of end of execution is reached - all you need is a static bool variable. If you want one thread to wait till another thread reach a point in execution you might want to use WaitEvents (AutoResetEvent or ManualResetEvent). Iflyyou need to wait for multiple waitHandles you can use WaitHandle.WaitAll or WaitHandle.WaitAny.

Dror Helper
i already have a List, that i could check if it contains more than zero items. The problem is telling the main thread that the list has more than zero items, or alternatively that a boolean has been set, or an Event, Mutex, or Semaphone has been set.
Ian Boyd
A: 

Take a look at Andrew D. Birrell Introduction to Multithreading in C#. I just finished it and it does a great job in explaining what objects and methods to use in the System.Threading namespace. It also looks at the common pitfalls found in concurrency.