Threading is the concept of doing two things at the same time or at least the appearance of it, for example we all remember applications that would freeze when long running tasks are required.
using System;
using System.Threading;
namespace pc.threadingExample
{
class Program
{
static void
LongRunningMethod()
{
Thread.Sleep(5000);
Console.WriteLine("Hello");
}
static void Main(string[] args)
{
char inputChar;
do
{
Console.WriteLine("1)say hi \n0)exit\n");
inputChar = Console.ReadKey().KeyChar;
if (inputChar == '1')
LongRunningMethod();
} while (inputChar != '0');
}
}
}
if we take a look at the above program when we call our long running method we lose responsiveness for 5s; our app looks as if it's frozen which is a piss poor user experience, what we need to do is launch our long running method on a separate thread.
using System;
using System.Threading;
namespace pc.threadingExample
{
class Program
{
static void
LongRunningMethod()
{
Thread.Sleep(5000);
Console.WriteLine("Hello");
}
static void Main(string[] args)
{
char inputChar;
do
{
Console.WriteLine("1)say hi \n0)exit\n");
inputChar = Console.ReadKey().KeyChar;
if (inputChar == '1')
new Thread(LongRunningMethod).Start();
} while (inputChar != '0');
}
}
}
however if you do want to ignore your threads you just have to set the IsBackground property to true.
using System;
using System.Collections.Generic;
using System.Threading;
namespace pc.threadingExample
{
class Program
{
static void
LongRunningMethod()
{
Thread.Sleep(5000);
Console.WriteLine("Hello");
}
static void Main(string[] args)
{
char inputChar;
do
{
Console.WriteLine("1)say hi \n0)exit\n");
inputChar = Console.ReadKey().KeyChar;
if (inputChar == '1')
{
var t = new Thread(LongRunningMethod);
t.IsBackground = true;
t.Start();
}
} while (inputChar != '0');
}
}
}
and that's it, now our application will ignore any running threads a just shutdown when we want it to. however let's say that we want to join our long running threads back to our main, well all we'd have to do is store a reference to each thread and then just call it's join method
using System;
using System.Collections.Generic;
using System.Threading;
namespace pc.threadingExample
{
class Program
{
static void
LongRunningMethod()
{
Thread.Sleep(5000);
Console.WriteLine("Hello");
}
static void Main(string[] args)
{
char inputChar;
var threads = new List<Thread>();
do
{
Console.WriteLine("1)say hi \n0)exit\n");
inputChar = Console.ReadKey().KeyChar;
if (inputChar == '1')
{
var t = new Thread(LongRunningMethod);
t.IsBackground = true;
t.Start();
threads.Add(t);
}
} while (inputChar != '0');
foreach (var t in threads)
{
Console.WriteLine($"thread:{t.ManagedThreadId} joining");
t.Join();
}
}
}
}