Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing a thread

I have the following code fragment from a project I'm working on:

public void Start()
      {
         Thread t = new Thread(NotifyIfNecessary);
         Threads.Add(t);
         t.Start();
         t.Abort());

      }

What I want is that thread 't' should execute the method NotifyIfNecessary and abort only after the method has completed execution. In my current code, t.Abort() gets called prematurely.

like image 834
xbonez Avatar asked Dec 02 '22 04:12

xbonez


1 Answers

This is being caused because you're creating a new thread and starting it, then immediately killing it from the thread that you just created it in by calling the Thread.Abort() method. You don't need to do this; your thread will finish when NotifyIfNecessary has completed execution. Just remove the line t.Abort(); and your code should work properly.

like image 152
Donut Avatar answered Dec 04 '22 02:12

Donut