Use of threads in C# without storing in variable

Asked

Viewed 116 times

1

If I start a thread anonymity:

new Thread(chat).Start();

and would again instate it in the same way, it overlap the other thread? If not, how should you give a "Kill" in the first thread in question.

  • Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, check out [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

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

Each time this code is called will create a new instance, ie a new object, carrying a new thread. They don’t get confused.

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

In this case the thread will end when the method finishes its execution, providing the object of the thread for further 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 executing under normal conditions. Have an example on documentation.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.