Question from a Beginner (and where's some sample code?)

Apr 23, 2014 at 12:22 PM
Edited Apr 23, 2014 at 12:41 PM
Stephen, I was reading articles by Stephan Toub to get up to speed on the TPL and luckily before my head exploded I found your BLOG. I've read possibly most of it. Thank you for your great effort in making this difficult subject understandable and usable. That said, I developed some understanding at this point and figured I try out your AsynchEx Library. Microsoft libraries like TPL seem always to be missing something and I figured your library probably smoothed some stuff out. Unfortunately, somewhere I missed finding much of any source code. I've looked around a lot and somehow I've missed it. On Wednesday, November 6, 2013 at 12:38:00 PM EST on your BLOG, you even say that you have posted examples, but where? The CoedePlex Documentation page has information, but I cannot use it to get started.
Really, with that comprehensive BLOG, I bet that somewhere there are useful examples, but where?
Thanks much, Mike Breeden

PS. What I am trying to do is very straight forward, very like old threading, but there is one place I make a database call to an iSeries and it could take up to a minute, so my plan is to put that task in an async/await block. I am also hoping I can use a CancellationToken, but it is a non-polling situation, so I am not sure. I think I understand that you can use that to throw an exception in the thread, but I need to experiment to see how it really works. I'm very good at expanding a project once I get it started, but I just can't get started with your library.

Oh, one particulalry interesting comment on the documentation page was:
Extension Methods
CancellationToken.AsTask will create a task that is canceled when a CancellationToken is canceled.
Yah, that's the ticket! Where can I find more about it?
Coordinator
Apr 23, 2014 at 6:47 PM
First off, I like to make a distinction between the TPL (which is usually used for parallel processing) and async/await (which is used for asynchronous processing). They do share a Task type but that's about it.

Most of my async/await posts are just taking things that Stephen Toub has written and rewording them, or expounding on them, or looking at them in a different way. He's the real genius in the room and if anything I say disagrees with one of his posts, he's right. As I say in my talks, "I don't know everything; my name is Stephen Cleary, not Stephen Toub." :)

I have found that both the TPL and the async/await support is sufficient for most scenarios. My AsyncEx library just collects a few useful types that extend async/await slightly. To be clear, I have written a number of async/await apps that do not use AsyncEx, as well as a number that do use it.

The examples that I mention on my blog on 2013-11-06 are inline in that blog post. That post was the first of a three-part series where I show (by example) how Task.Run should and should not be used. Those blog posts don't have to do with AsyncEx at all. Unfortunately, AsyncEx does not have much sample code.

For your scenario, if your database supports asynchronous operations, then you should just use that. If their .NET API supports the task-based asynchronous pattern (a Task-returning method ending in "Async"), then use that; otherwise, they probably support the asynchronous programming model (Begin*/End* with IAsyncResult), and you can wrap that into a TAP method.

As far as cancellation goes, I have written very little on the subject mainly because the MSDN documentation is so good. Note that cancellation is cooperative; if your database API doesn't support cancellation, then you can't actually cancel it (it does not work like a Thread.Abort or anything like that). The best you could do in that case is just ignore the result if the operation is canceled.

If you have any further async/await/cancellation questions, feel free to post on Stack Overflow. I keep an eye on the async/await questions there, and there are a number of very smart answerers on that site.

-Steve