Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, CP Community.
i am preparing for "MSCSD 70-483 Programming in C#" and in that context i recently learned a bit about Tasks, await and async, so i am absolutely new to that topic.
What i would like to know is whether i understood the underlying concept in the correct manner or not.
As an exercise i wrote a little backup program that copies files and folders from a source path to a destination path. Nothing complex, just to figure out, what the benefits are and what the downsides are or rather what it can NOT fulfill.
After the process of copying i log some data in a database. i want to have these two steps being processed successively.

For me the ultimate benefits are controlling the program flow, decide whether it should be processed parallel or not and managing "thread usage".

For convinience i selected an "oldskool" WinForm-project in VS2019.
My question is:
Am i right that this concept is NOT used to ensure UI Responsiveness?
If i want to achieve this i still need to use a background worker, don´t i?
I am a little bit confused, so any help from you to enlighten me is highly appreciated. :-)
Thanks in advance,
kind regards :-)
Clodetta

What I have tried:

i learned something about Tasks, await and async and am not quite sure whether i understood it correctöy
Posted
Updated 17-Jul-19 4:34am

Quote:
Am i right that this concept is NOT used to ensure UI Responsiveness?
No - async / await is great at ensuring UI responsiveness, if used correctly.

Things which are CPU-bound, or which run synchronously, can still cause problems. Eg: calling Thread.Sleep on the UI thread will freeze the UI. But there are usually alternatives that will keep the UI responsive - eg: using Task.Delay instead of Thread.Sleep. And if all else fails, you can push the CPU-bound code onto a background thread by using Task.Run.

Unfortunately, the System.IO classes don't provide asynchronous methods for enumerating or copying files, so you're unlikely to see any benefit from async in this case.
 
Share this answer
 
Hello, Richard.
Thank you for your reply!


This explains perfectly why my App hangs as long as it copies files and folders and why i do not see any effects on the UI although I am using
C#
Task.Run
.

Again, thank you for your help.
*thumbs up*
:-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900