Using threads in C # without storing in variable

1

If I start an thread anonymously:

new Thread(chat).Start();

and then instantiated it in the same way, does it overlap the other thread ? If not, how should you kill the first thread .

    
asked by anonymous 10.04.2016 / 12:22

1 answer

1

The question does not give much context, I will try to guess some things.

Each time this code is called it will create a new instance, that is, a new object, with a new thread . They are not confused.

In this way you can not have any control over the instance since you can not refer to it. If you need this, it's very simple, play on a variable to have a reference in the code. There is no reason not to do this.

In this case, the thread will terminate when the method finishes its execution, making the thread object available for collection.

Even if the instance was "stored" in a variable, it should not "kill" it directly. What should be done is to send an instruction (through a variable that can be accessed from outside the method) so that the method stops performing under normal conditions. You have an example in documentation .

Or think about using Task which is much better than raw thread .

    
10.04.2016 / 14:10