C# Threads, Tasks, Multi-threading & UI Cross-threading

AngelSix · 101K views · 2018 · 1:07:04

Open on YouTube

Software architecture & designPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about hanging UIs, async/await confusion, and cross-thread UI access in C#, because it explains threads from the ground up.

Recommended by 2 people across 2 different months, 2018 to 2021.

What it is

A just-over-an-hour C# tutorial by AngelSix that starts from what a thread is and builds up to the thread pool, Tasks, and the problem of editing UI controls from a non-UI thread. It covers how to create one or many threads, how threading affects speed and program flow, and how to fix cross-thread UI errors.

What people said

Recommends angelsix · 2018-04 · Stack Overflow · on “Trying to find why my PostAsync(URL,BODY).Results will Time out, but only in .NET 4.6”

The first thing I would do is to remove the .Result you are doing everywhere to forcefully drop async Tasks back to synchronous returns. It is bad and could introduce locks as well as other issues. Instead a simple fix to convert it to true async and still allow the console to wait is like this: using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var tcs = new TaskCompletionSource<bool>(); string reply = string.Empty; Task.Run(async () => { try { using (var httpClient = new HttpClient()) { var httpContent = new StringContent("{\"Image\":\"busybox\"}", Encoding.UTF8, "application/json")

Recommends angelsix · 2018-04 · Stack Overflow · on “c# form hangs when new thread starts”

Not to advertise :P but I have done a video detailing Threads it might help you understand them better [this video]

Recommends angelsix · 2018-04 · Stack Overflow · on “Attempted to read or write protected memory when querying database”

Issues with the code The only issues I can identify in the code as you have provided (as some information is lacking) is that every time you type a character, you do a database query and you do that on the same thread (the one the UI is on) and so if the query took 2 seconds to complete, your typing would take 2 seconds per character to type. Very bad experience for the user. For the MySQL statement itself, there is nothing inherently wrong with it, although I do believe this is the cause of your issue. Fixing the UI thread Firstly I would say to understand why you have the thread issue you should first understand threads. I've made a video on it here [this video] and here [LINK] After you unders

Recommends Fiona · 2021-07 · Stack Overflow · on “Difference between Async/Await and Dispatcher”

This are the Video 1. C# Threads, Tasks, Multi-threading & UI Cross-threading [this video] 2. C# The UI Thread and STA [LINK] 3. C# Tasks Async Await [LINK] if you want ti learn these are just it

Cites wyllisMonteiro · 2019-12 · Stack Overflow · on “Error during transfering Bytes xamarin forms”

@JackHua-MSFT I need to use await because I use ReadStringAsync. I think I have to use thread to download each image. I'm looking a video : [this video]