Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Is there any course that explains the basic concepts of async programming?

I need to learn how it works in C# but I can't understand it completely. Is there any course that explains this for other programming languages?

4 Answers

Steven Parker
Steven Parker
231,210 Points

I'm not aware of any, but it is a cool idea for a future course!

I've seen some related concepts presented in some of the other language courses, such as callbacks and promises in Javascript. But I haven't seen any courses specifically dedicated to asynchronous (concurrent/threaded/parallel, etc.) programming concepts.

It does sound like a neat idea, it would be nice to see some developed for the future.

James Churchill
STAFF
James Churchill
Treehouse Teacher

Calin,

Asynchronous programming with C# is a topic that we're planning on covering in a future course. You can see our roadmap at https://trello.com/b/WfGXKHAo/treehouse-c-net-content-roadmap

Thanks ~James

mattmilanowski
mattmilanowski
15,323 Points

Thanks James. I arrived here looking to ask the same thing. I'm currently working on a project involving async and await. My hang up is reporting back progress to the UI. For now I'll scour the internet and get it figured out. I always go back to my Treehouse courses though to further solidify my knowledge of the subject when it's something covered here.

James Churchill
James Churchill
Treehouse Teacher

Matt,

What specifically is the background task that you're doing? Also, is this for a web app or something else?

Thanks ~James

mattmilanowski
mattmilanowski
15,323 Points

This is a desktop program using windows forms. The 4 long running tasks being run are:

*Pulling 2 columns from a group of 4 to 6 csv files of the same format (weekly UPS shipping bills) and adding them to a list.

*Loading another csv of different format (a shipping manifest program log) into another list

*Combining / joining these 2 lists into another list that lays out all data in one place.

*Writing out this combined list to a csv file for the accounting department to work with.

The csv file that this generates shows what the shipping manifest quoted for a shipping rate vs what UPS actually charged to ship each package. UPS has a lot of surcharges, fees and adjustments. Sometimes what the shipping program thinks you paid to ship is not what UPS ends up charging you and you only know the true UPS ship charge after they close their weekly invoice. The output csv file can contain between 60,000 and 100,000 rows so it takes a while.

My goal for reporting progress was to let the user know which one of the 4 steps it's on and also an 'X out of X records processed' type of message. At the moment the program works and provides meaningful data, which is the most important part. I was just trying to report progress to expand my knowledge of the Async process.

A future course using an example similar to this would be awesome! I'm bias though! On that same note, windows form app best practice courses would be wonderful too. Many tutorials and books only seem to focus on console apps. Nearly everything I do is windows form based so I always take what I learn and adapt it to windows forms.

Thanks for reading! -Matt

James Churchill
James Churchill
Treehouse Teacher

Matt,

I'd take a look at the .NET Framework's BackgroundWorker class.

https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

This is just one option of course; you could also look at the Task Parallel Library, though I think you'll find that the BackgroundWorker class is a little easier to work with.

Something that you'll need to pay particular attention to, is this bit from the MSDN documentation:

"You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events."

Only the thread that owns the UI can update the UI. Trying to update the UI from the background thread will cause an exception to be thrown.

Adding this level of polish to your app is a great challenge. Your users will definitely appreciate it :)

Thanks ~James