Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispose thread in c#

I have used threading in my web application which I have mentioned below:

var t1 = new Thread(F1);
            t1.IsBackground = true;
            t1.Start();

            var t2 = new Thread(F2);
            t2.IsBackground = true;
            t2.Start();

            var t3 = new Thread(F3);
            t3.IsBackground = true;
            t3.Start();

            var t4 = new Thread(F4);
            t4.IsBackground = true;
            t4.Start();


            t1.Join();
            t2.Join();
            t3.Join();
            t4.Join();

This is working fine and giving me the desired output.

Do I need to kill/Dispose the thread after this, if yes then how ? Please guide.

I have told that if I do not dispose it, it might raise performance issue.

like image 814
Zerotoinfinity Avatar asked Oct 25 '11 22:10

Zerotoinfinity


1 Answers

The call to Join() is what de-allocates the thread. You don't have to do anything else. Just make sure that the threads clean up any resources they might be using before they exit.

That said, I would urge you to look into using the thread pool or the Task Parallel Library (TPL) rather than explicitly managing threads. They're easier to use, and handle this kind of thing much more smoothly.

like image 77
Jim Mischel Avatar answered Nov 14 '22 22:11

Jim Mischel