1

I have a Window in WPF and user can start very long operations on it. User must be able to cancel those operations.

All of my operations are in separate threads. So my question is: Can I terminate all threads that are started from that Window, without killing UI thread obviously, at any time?

On places where I need to do long operations threads were created and started like this

 Thread thread =
            new Thread(
                new ThreadStart(
                    delegate
                    {...}));
        thread.Start();

How to pass that object to it? is it possible? If it is important at all I do not care about graceful closing of threads, they can be killed, it would still be a solution. Is window object aware of threads to whom it is parent?

Thank you in advance.

Rouz
  • 1,247
  • 2
  • 15
  • 37

1 Answers1

0

Typically you won't want to create/destroy threads. There's much more overhead when creating a Thread every time you need one than there is in thread pools and Tasks (This applies, like specified, when you need to create a significant number of Threads in the lifetime of your processes).

The preferred approach (especially if you're using .Net 4.0, or even better 4.5) is to use Tasks.

There's is tons of documentation on how to use Tasks, and how to cancel them. @xxbbcc posted a link in a comment on your question.

However, if you still think that dealing with Threads is your best choice, you could keep a track of all the threads. Then whenever you (as a developer) or your user determines they want to kill the thread, you can just iterate through the threads and call the Abort() method on them.

public class MyExampleClass
{
  private List<Thread> MyThreads { get; set; }

  public MyExampleClass()
  {
    MyThreads = new List<Thread>();
    InstanciateThreadsWithSomeSuperImportantOperations();
  }
  private void InstanciateThreadsWithSomeSuperImportantOperations()
  {
    var thread = new Thread();
    // some code here 

    MyThreads.Add(thread);
  }

  public void KillAllThreads()
  {
    foreach (var t in MyThreads)
    {
      if (t.IsAlive)         
        t.Abort(); // Note this isn't guaranteed to stop the thread.
    }
  }
}
Cameron
  • 2,574
  • 22
  • 37
  • 2
    [Don't recommend `Abort` please](http://stackoverflow.com/questions/27561976/check-if-thread-finished-its-method-before-killing-it-c-sharp/27562048#27562048) – Sriram Sakthivel Dec 23 '14 at 15:36
  • Using abort or not is another issue here. Of course I can create a list of threads and kill them all, and it looks that i will have to do exactly that. It is actually my question - can i do it without manually having to watch about threads that are running(have they stopped, remove them from list etc...). And of course if i can do it without a massive refactoring of my code – Rouz Dec 23 '14 at 15:38
  • @SriramSakthivel I'm trying to not recommend using Threads at all, but I don't see another way to kill a thread. – Cameron Dec 23 '14 at 15:42
  • 1
    There exist a way, it is right there in the link I provided. Use TPL with cancellation token if .net 4.0 or greater. Otherwise use a volatile flag and check it. – Sriram Sakthivel Dec 23 '14 at 15:42
  • @SriramSakthivel as I see, this link explains what is wrong with answer Cameron provided, but unfortunately it does not answer my needs. – Rouz Dec 23 '14 at 15:46
  • @Rouz It explained it. Please read *Let the thread decide when it should complete* section. If you need code help, please provide your code. – Sriram Sakthivel Dec 23 '14 at 15:47
  • @SriramSakthivel no it did not answer my question. You obviously are right, but that is not what I asked. Question is, is a Window aware of it's children threads? If it is, I will close them as I please (like you said ofc). But if not i have to do massive refactoring and modifications - which i tried to avoid. – Rouz Dec 23 '14 at 15:56
  • 1
    @Rouz *is a Window aware of it's children threads* Answer is no. Window doesn't know about threads, not it should. So you need to keep a list of threads you create. – Sriram Sakthivel Dec 23 '14 at 16:00
  • @SriramSakthivel ok, put it like answer if you want me to accept it. Thanks – Rouz Dec 23 '14 at 19:14