2

I am working on a WPF application and I am running into freezing issues, by now I have learned that its a single threaded application, but i am getting confused with terminologies

I simple terms

Difference between Async/Await and Dispatcher

I want to explain my point of view so its clear on both ends my misunderstanding , until now I have learned that most applications are STA (in c#), and you have to use asynchronous programming, which can very well involve

  • A single thread that shifts work on the UI thread by prioritizing work that has already completed and then using a worker thread to notify the UI thread that the heavy work is done
  • Also a truly multi threaded application where a thread needs to be created to complete the task on it

I think for most part my concept is at least solid to say the least where I lack is applying these abilities

for instance i though that previously to enable asynchronous programming we had to use delegates to be called from the main method, which evolved into a dispatcher cutting of delegates which further evolved into async and await cutting 'ANY SINGLE USE OF' dispatcher (even that literal keyword)

so all i used were "async" word at the declaration of the function, followed by the "task", then awaiting some intense process by sticking an "await" word before it finally encapsulating that intense work with Task.Run(() => IntenseWork())

but now I am confused that you have to use Dispatcher word because UI elements can only be accessed by dispatcher , and use Dispatcher.Invoke(IntenseWork()), then there is Dispatcher.Begininvoke and Dispatcher.AsyncIncoke

DOUBT:

in which case async/await and task.run isn't going to be enough and is task parallel library even going to be used here


I asked questions and reserched and am stuck at these conclusions, these r my previous questions

Asynchronous Navigation

Using async and await to achieve asynchronicity in example problem

Fiona
  • 77
  • 5
  • So you lack understanding of 1) [what async is](https://blog.stephencleary.com/2012/02/async-and-await.html), and 2) [why Invoke is required](https://stackoverflow.com/q/142003/11683) for accessing UI from different threads. These are unrelated concepts. Invoke is always required when a different thread is involved, regardless of whether or not the involvement of that thread was caused by `async`. And `async` [does not](https://stackoverflow.com/q/17661428/11683) necessarily involve other threads, unless you specifically ask it to. – GSerg Jul 11 '21 at 08:38

1 Answers1

3

You are asking about two different technologies from different periods.

The common problem is that the UI is single threaded and should only be accessed from the main (UI) thread.

You can and should offload non-UI work to another thread as much as possible. But there usually are results that need to be displayed afterwards.

The old (but still valid) approach is to hand a delegate to Dispatcher.Invoke(), or Control.Invoke() in WinForms.

The newer approach, applicable to Task.Run() and all DoSomethingAsync() I/O methods is to use await:

// use async void only for eventhandlers
async void LoadButton_Click(object s, RouteEventArgs e)
{
   // get input from controls here
   var results = await Task.Run(() => HeavyWorkWithoutUI());
   // update UI with results here
}

The WPPF and WinForms support is that your toplevel methods will run on the UI thread before and after an await. The heavy method will run on another thread. During the await your UI remains responsive.

So await is just a little more convenient, letting you write more readable code with less effort.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Thank you for being so descriptive, But this is a problem that i just couldn't really understand and had to understand from a video, for anyone struggling with WPF or C# you have to check out angelsix, – Fiona Jul 16 '21 at 06:51
  • to explain my problem, is that I was misunderstanding the UI task and the strain Intensive task as one (this is the one written in c#), this was also because I was performing the a few UI operation using C# code rather thatn XAML. Inshort Dispatcher is used but only in appropriate places like with enclosed within a Task.Run, and on top of that Async is used and task is Awaited, Dispatcher is Used to jump back to the calling thread which mean, and UI element such as a button or a grid, as this was created on the UI thread, but since we used Task.run it jumped to another thread – Fiona Jul 16 '21 at 06:57
  • This are the Video 1. [C# Threads, Tasks, Multi-threading & UI Cross-threading](https://www.youtube.com/watch?v=XXg9g56FS0k) 2. [C# The UI Thread and STA](https://www.youtube.com/watch?v=S0jPzb9kk3o) 3. [C# Tasks Async Await](https://www.youtube.com/watch?v=CzgDxdwTJds) if you want ti learn these are just it – Fiona Jul 16 '21 at 06:57
  • Also watch through the same order – Fiona Jul 16 '21 at 07:02