Don't use threads, use tasks instead. 03-29-2016, 01:22 AM
#1
Alright, while freelancing and "mentoring" people C#, ive seen a lot of people asking me to help them multi thread their applications and what I often see is the stupid way of doing, using the System.Threading.Thread lib and it's quite awful! Developers should rather be using the System.Threading.Tasks lib as it's way more efficient alongside of being much easier to manage.
Let's start.
Here is a bad example of multithreading:
Now, while this still is asynchronous, it is considered obsolete by developers and microsoft.
Here is a good example of "multithreading" (note: this isn't necessarily multithreaded as it is mostly done asynchronously within the main thread/ task threadpool)
Alright that's it for now because im kinda tired but ill add more sometime if people are interested.
Also if you want to know more about efficient parallel, asynchronous programming just google:
c# asynchronous programming
c# parallels
c# tasks
and here are some links and videos I found useful:
I also recommend that you learn basic lambda before jumping into all of this because it makes this (and c# programming) must easier and faster.
Let's start.
Here is a bad example of multithreading:
Code:
static void Main(string[] args)
{
Thread thread = new Thread(() =>
{
Console.WriteLine("This is a bad example of threading");
});
}Here is a good example of "multithreading" (note: this isn't necessarily multithreaded as it is mostly done asynchronously within the main thread/ task threadpool)
Code:
static void Main(string[] args)
{
Task task = Task.Factory.StartNew(() =>
{
Console.WriteLine("This is a good example of multithreading");
});
}Alright that's it for now because im kinda tired but ill add more sometime if people are interested.
Also if you want to know more about efficient parallel, asynchronous programming just google:
c# asynchronous programming
c# parallels
c# tasks
and here are some links and videos I found useful:
Spoiler:
https://www.youtube.com/watch?v=RhLLxew4-TY
Basic understanding of a thread concept:
https://www.youtube.com/watch?v=mkkcqQlIpeQ
More onto tasks and asynchronous stuff:
https://www.youtube.com/watch?v=IONqMWGn9-w
https://www.youtube.com/watch?v=TRXFPPSM3zQ
https://www.youtube.com/watch?v=WjHhQYEBE_s
MSDN:
https://msdn.microsoft.com/en-us/library....110).aspx
https://msdn.microsoft.com/en-us/library/x13ttww7.aspx
https://msdn.microsoft.com/en-us/library/hh156513.aspx
https://msdn.microsoft.com/en-us/library/hh191443.aspx
https://msdn.microsoft.com/en-us/library/hh696703.aspx
https://msdn.microsoft.com/en-us/library/jj155756.aspx
https://msdn.microsoft.com/en-us/library....110).aspx
https://msdn.microsoft.com/en-us/library....110).aspx
Basic understanding of a thread concept:
https://www.youtube.com/watch?v=mkkcqQlIpeQ
More onto tasks and asynchronous stuff:
https://www.youtube.com/watch?v=IONqMWGn9-w
https://www.youtube.com/watch?v=TRXFPPSM3zQ
https://www.youtube.com/watch?v=WjHhQYEBE_s
MSDN:
https://msdn.microsoft.com/en-us/library....110).aspx
https://msdn.microsoft.com/en-us/library/x13ttww7.aspx
https://msdn.microsoft.com/en-us/library/hh156513.aspx
https://msdn.microsoft.com/en-us/library/hh191443.aspx
https://msdn.microsoft.com/en-us/library/hh696703.aspx
https://msdn.microsoft.com/en-us/library/jj155756.aspx
https://msdn.microsoft.com/en-us/library....110).aspx
https://msdn.microsoft.com/en-us/library....110).aspx
I also recommend that you learn basic lambda before jumping into all of this because it makes this (and c# programming) must easier and faster.



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)






