-3

As it is clear from the title
I want remove automatically threads from threads list when thread jobs done.
like:

Thread thr;
if(thr.done)
{
    threadlist.remove(thr);
}
  • 1
    my recommendation don’t do it. actually use the thing you have tagged the question with threadpool - https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool | https://stackoverflow.com/questions/14515207/whats-the-proper-way-to-use-a-threadpool | https://stackoverflow.com/questions/230003/thread-vs-threadpool – Rand Random Oct 08 '22 at 12:48
  • Is it an option to add some code at the end of each thread, so that each thread removes itself from the list? – Theodor Zoulias Oct 08 '22 at 12:53
  • Does this answer your question? [what's the proper way to use a ThreadPool?](https://stackoverflow.com/questions/14515207/whats-the-proper-way-to-use-a-threadpool) – BurnsBA Oct 08 '22 at 16:19
  • What you're doing is likely to not be a good method, so the question you're asking is not actually what you want to do. Can you tell us what you're trying to retrieve. – Enigmativity Oct 10 '22 at 06:30

1 Answers1

-3

you can check if the thread is finished with IsAlive and call this function in a timer:

var _timer = new Timer();
_timer.Interval = 30*1000; // specify interval time as you want
_timer.Tick += new EventHandler(timer_Tick);

void timer_Tick(object sender, EventArgs e)
{
   foreach (var thr in threadList)
   {
       if (!thr.IsAlive)
       {
            threadlist.Remove(thr);
       }
   }
}
_timer.Start();

A better solution is to use a try-finally block in thread to determine when it is finished processing. see this example:

private static int lastThreadID=0;  //We assign an ID to each thread
protected static object _lock = new object();
var thread = new Thread((tID) =>
{
    try
    {
       work();
    }
    finally
    {
       removeThread((int)tID );
    }
});
lock (_lock)
{
    threadlist.Add(Tuple.Create(++lastThreadID, thread));
}

thread.Start(lastThreadID);

Action work = () =>
{
   for (int i = 0; i < 20; i++)
       Thread.Sleep(1000);
};


private static void removeThread(int tID)
{
   lock (_lock)
    {
      threadlist.RemoveAll((x) => x.Item1 == tID);
    }
}
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • No, no, no. Abort is never the answer. It s dangerous and has been removed from the newer versions of .NET. – Enigmativity Oct 10 '22 at 06:29
  • @Enigmativity Regarding thread, your answer is correct. I only answered the question that how to terminate a thread and did not comment on whether it is a good way or not... Now I edited the answer and suggested the task.Run() – Hossein Sabziani Oct 11 '22 at 15:56
  • Hossein the OP has not asked how to terminate threads *before* they are done. They asked how to remove threads from a list, *after* they are done. – Theodor Zoulias Oct 11 '22 at 17:21
  • @TheodorZoulias I also said check first to see if the thread is alive or not, if not, remove it from the list. Abort the thread before deleting. This function must be called in a timer. Because the answer to the question has been accepted, I cannot delete it...:( – Hossein Sabziani Oct 11 '22 at 19:59
  • 1
    Hossein my suggestion is to edit the answer and remove all references to `Thread.Abort`. The OP has not asked for it. Preserving the reference won't earn you any reputation, only downvotes. This method is not supported ιn the currently evolving .NET platform (.NET Core and later). It is only supported ιn the stagnate .NET Framework. – Theodor Zoulias Oct 11 '22 at 20:11
  • 1
    @hosseinsabziani - saying fire-and-forget is an answer to removing threads from a list is not an answer without giving it any context. It's like saying gardening is a great way to go vegan. There might be a link, but without explanation the link is just a guess. – Enigmativity Oct 11 '22 at 23:40
  • @Enigmativity I have edited the answer according to the question and I would like to know your opinion...tnx – Hossein Sabziani Oct 12 '22 at 05:01
  • @Theodor Zoulias I have edited the answer according to the question and I would like to know your opinion...tnx – Hossein Sabziani Oct 12 '22 at 05:02
  • The first solution with the timer and the `IsAlive` has some formatting issues. The `t.Start();` seems out of place. The second solution with the try-finally block is fragile because of the manually assigned ids. It is also incorrect because the `threadlist` is modified by multiple threads concurrently without synchronization. – Theodor Zoulias Oct 12 '22 at 05:38
  • Btw the `t` is not a great name for a variable, when you are working with both **t**imers and **t**hreads. The culture of C# is different than C. In C# we like to be verbose. The preferred name for a C# variable that represents a thread is `thread`, and for a timer, `timer`. – Theodor Zoulias Oct 12 '22 at 05:42
  • @TheodorZoulias For someone who does not want to accept it is useless to explain. First, the ID can be through a Global variable and id generator function and not manual. If the list edit with multiple threads, we should consider thread safety and lock the list. That I just got an example – Hossein Sabziani Oct 12 '22 at 06:01
  • Hossein sorry, I am not upvoting posts with obvious mistakes. My suggestion is to try to earn upvotes by improving your post. – Theodor Zoulias Oct 12 '22 at 06:12
  • 1
    @hosseinsabziani - Your answer is getting worse. The OP said nothing about Windows Forms - so don't use that timer. Also, removing items from a list from multiple threads is bad! – Enigmativity Oct 12 '22 at 08:50
  • this answer updated again to be thread safe for list add and remove///\\\ – Hossein Sabziani Oct 13 '22 at 12:53