Jump to content

- - - - -

C# 5 Ascnc Functions From Backgroudworker Threads

  • Please log in to reply
1 reply to this topic

#1 MartinHT


    New Member

  • Members
  • Pip
  • 1 posts

Posted 07 September 2012 - 11:57 PM

I have just purchased the new C# 5 books, and I'm loving it! It has, however, raised some questions.

For instance, I have a large project that has recently been updated to Net 4.0, to which I have added the Async Targeting Package.

My question is: I have lots of BackgroundWorker's that load SQL data (some can take several seconds), and was wondering if these are candidates to convert to async/await functions.

Comments and advise would be appreciated.

#2 JoeAlbahari


    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 529 posts
  • Gender:Male
  • Location:Perth, Australia

Posted 09 September 2012 - 07:21 PM

As with any new technology, you don't usually need to rush out and convert all existing code to use the new features.

A good time to do so is when you're enhancing or refactoring the code. For instance, suppose you have code in a BackgroundWorker's DoWork event that you want to re-use elsewhere in your program, or you want it to access shared state safely without resorting to locks. This would be a good time to refactor it into asynchronous functions.

Another point is that when it comes to accessing the database itself, asynchronous database API functions may not exist if you're working at a higher level than ADO.NET (e.g., an object/relational mapper). If this is the case, it's nearly always acceptable to wrap the function that accesses the database synchronously in Task.Run to make it asynchronous. You'll still be able to write asynchronous call graphs and get the thread-safety and simplicity benefits discussed in Chapter 14.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users