C
C#7mo ago
Dachi

✅ 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
Dachi
Dachi7mo ago
c#
Temp();

static void Temp()
{
var main = Thread.CurrentThread;
RunningThread(main);
}

static void RunningThread(Thread mainThread)
{
var thread = new Thread(() =>
{
while (true)
{
Console.WriteLine("Running thread is alive");
Thread.Sleep(2000);
Console.WriteLine(mainThread.IsAlive + " is main thread alive");
}
});
thread.Start();
}
c#
Temp();

static void Temp()
{
var main = Thread.CurrentThread;
RunningThread(main);
}

static void RunningThread(Thread mainThread)
{
var thread = new Thread(() =>
{
while (true)
{
Console.WriteLine("Running thread is alive");
Thread.Sleep(2000);
Console.WriteLine(mainThread.IsAlive + " is main thread alive");
}
});
thread.Start();
}
JP
JP7mo ago
in the screen shot it behaves differently
Can 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
Dachi
Dachi7mo ago
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
JP
JP7mo ago
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 👀
Dachi
Dachi7mo ago
i am not killing it by force. It stops by itself, as code demostrates.
cap5lut
cap5lut7mo ago
as long as there are any non-background threads running the process continues execution. it doesnt matter if the main thread finished execution earlier
Dachi
Dachi7mo ago
So main thread is not that main?
cap5lut
cap5lut7mo ago
its simply the only thread created at process start, thats it
Dachi
Dachi7mo ago
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.
cap5lut
cap5lut7mo ago
each thread has its own stack
Dachi
Dachi7mo ago
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
cap5lut
cap5lut7mo ago
u can easily observe this with the following program:
var mainThread = Thread.CurrentThread;
var secondaryThread = new Thread(() => {
while (mainThread.IsAlive)
{
Thread.Sleep(1);
}
Console.WriteLine(mainThread.IsAlive);
});
secondaryThread.IsBackground = false;
secondaryThread.Start();
var mainThread = Thread.CurrentThread;
var secondaryThread = new Thread(() => {
while (mainThread.IsAlive)
{
Thread.Sleep(1);
}
Console.WriteLine(mainThread.IsAlive);
});
secondaryThread.IsBackground = false;
secondaryThread.Start();
(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
Dachi
Dachi7mo ago
Sure thanks for clearification
cap5lut
cap5lut7mo ago
glad i could help o7 if u feel like ur questions are answered, dont forget to $close the thread (pun not intended)
MODiX
MODiX7mo ago
Use the /close command to mark a forum thread as answered