✅ Threads in c#
I am wondering if it is possible to kill main thread while other thread is still being executed? Cuz as i know additional threads are being created in the main thread thus killing main threads would mean to stop executing the code but in the screen shot it behaves differently.
15 Replies
in the screen shot it behaves differentlyCan you elaborate here? Differently how? I don't actually know the answer here. Not sure if having child threads prevents killing the main thread from c# land
sure
by differently i meant that main thread dies
But as i know in the stack main thread is the first one, thus it means that main thread has to die last
I can say killing threads outright is generally dangerous
I'm going to go see if I can discover what happens w/ main thread kills by fiddling / googling 👀
i am not killing it by force. It stops by itself, as code demostrates.
as long as there are any non-background threads running the process continues execution.
it doesnt matter if the main thread finished execution earlier
So main thread is not that main?
its simply the only thread created at process start, thats it
like isn't executing happen like in a stack. First method (main) executes other ones thus it means that at the end when the all other threads end only then main thread dies.
each thread has its own stack
ooooh
so they're equal in terms of importance?
right?
like it is not that main thread is the MAIN thread
i get it now
Thanks
u can easily observe this with the following program:
(note that for some reason this doesnt work on sharplab, nor the bot gives output)
but also generally speaking, u shouldnt care too much about threads, especially creating ur own, this is a quite costly operation and using the threadpool/task API instead is most of the time better
and just throwing more threads at something doesnt make it faster per se, especially cpu-bound tasks in more threads than you have logical processor threads will most likely slow down the whole thing
Sure
thanks for clearification
glad i could help o7
if u feel like ur questions are answered, dont forget to $close the thread (pun not intended)
Use the /close command to mark a forum thread as answered