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
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
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
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
Using async and await to achieve asynchronicity in example problem
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.