O'Reilly Forums: C# 5 Ascnc Functions From Backgroudworker Threads - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

C# 5 Ascnc Functions From Backgroudworker Threads Rate Topic: -----

#1 User is offline   MartinHT 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 07-September 12

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 User is offline   JoeAlbahari 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 529
  • Joined: 15-February 08
  • 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.

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users