C# The UI Thread and STA

AngelSix · 13K views · 2018 · 1:09:06

Open on YouTube

Software architecture & designC, C++, Rust, Go & systems languagesSoftware (other)tutorial

Why Stack Overflow recommends it

Stack Overflow users link it when explaining why the [STAThread] attribute is needed and why UI updates must go through the Dispatcher.

Recommended by 2 people across 2 different months, 2008 to 2022.

What it is

A long tutorial by AngelSix that digs into the Microsoft source code to show how WinForms and WPF enforce the STA threading requirement. It replaces WPF's normal startup to run custom code, then tests how Dispatcher objects get work onto the UI thread.

What people said

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 [LINK] and here [this video] 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 [LINK] 2. C# The UI Thread and STA [this video] 3. C# Tasks Async Await [LINK] if you want ti learn these are just it

Cites albertein · 2008-09 · Stack Overflow · on “How do I convert a .NET console application to a Winforms or WPF application”

Just add a new Winform, add the following code to your Main : Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); Also, be sure the [STAThread] attribute is declared above your Main function to indicate the COM threading model your Windows application will use (more about STAThread here [this video] ). Then right click your project and select properties and change the "Output type" to Windows application and you're done. EDIT : In VS2008 the property to change is Application type

Cites juwens · 2022-08 · Stack Overflow · on “New Thread with INotifypropertyChanged or ObservableObject for uptade the View dont work in WinUi 3”

It's the age-old problem that the UI can only be updated from the main-thread (it was created with). (By varying degree of tolerance in one or other ui framework.) Solution for WinUI would look something like this. public class PageInicialViewModel : ObservableObject { private int cont = 0; private string _textLabel = 0.ToString(); private Microsoft.UI.Dispatching.DispatcherQueue _dispatcherQueue; public PageInicialViewModel() { // must be called on ui thread _dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); } public string TextLabel { get => _textLabel; set => SetProperty(ref _textLabel, value); } public void updateScreen() { Task.Factory.StartNew(updateText