New Posts New Posts RSS Feed: AsyncSerialTask, preferred way to stop execution
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

AsyncSerialTask, preferred way to stop execution

 Post Reply Post Reply
Author
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Topic: AsyncSerialTask, preferred way to stop execution
    Posted: 29-Oct-2010 at 10:38am

We've been using AsyncSerialTask for quite a while now, but what has never been clear to me is how to stop the task prematurely when a problem occurs.  Consider the following pseudo-code:

 

AsyncSerialTask.Create()

     .AddExceptionHandler((x) =>

     {

          MessageBox.Show(x.Exception);

     }

     .AddAsyncAction((a) => CheckForDuplicateName(a, nameToCheck))

     .AddAction((b) =>

     {

          if (b.HasError)

               throw b.Error;

 

          // Results indicate an entity with the same name already exists in the database.

          if (a.Results.Count() > 0)

               throw new Exception("Name is not unique");

     }

     .AddAsyncAction*

     .AddAction*

     .AddAsyncSave*

     .Execute(null, (a) => { });

 

Now, obviously the code in AddAction(b) could probably be done internally to CheckForDuplicateName, but I broke it out for demonstration.  We want to stop the task from continuing if the name is a duplicate, so we check the results and throw a new exception, allowing the task ExceptionHandler to handle the problem.  This works, but the debugger likes to inform us that the exception is not handled when we do this (even though it is).  Optionally we could pass around a task process status in the event args and short circuit things that way too.  Is there another option?  What is the preferred way to handle this?

 

 

 

Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 29-Oct-2010 at 3:09pm
Hi Ken.
 
First, thanks for making such good use of the AsyncSerialTask. That is probably the hardest component to use in DevForce. As of DF 6.0.6 we are encouraging folks to use the Coroutine instead. See the DRC article on Coroutines. Works only for C# developers, btw.
 
We're all looking forward to the "async" and "await" keywords that were announced for C#5 and VB11 at PDC 2010.
 
Meanwhile, I believe you can short-circuit processing of task by setting the Continue flag to false in the AsyncSerialTaskException arg that arrives in your exception handler (the lambda that shows the MessageBox in your example). See IdeaBlade.EntityModel.AsyncSerialTaskExceptionArgs.
 
Hope that does it for you.
Back to Top
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Posted: 01-Nov-2010 at 8:41am
Thanks Ward.
 
We're still using version 6.0.4 at the moment, hopefully I'll manage some time to update to 6.0.6 soon, but for the time being we'll be using the AsyncSerialTask.
 
I didn't see a Continue flag in the AsyncSerialTaskExceptionArgs, and the compiler doesn't seem to know what a AsyncSerialTaskException is (assuming that I would need to cast the AsyncSerialTaskExceptionArgs.Exception to a AsyncSerialTaskException).  The only thing I could find was the Handled flag in the AsyncSerialTaskExceptionArgs, which is initially set to false but can be set to true, and if I'm reading the documenation correctly does the exact opposite of what I need to do.  To restate it, this does: When an exception occurs, and the exception handler handles the exception properly, go ahead and continue executing the task.  Whereas what I was looking for is:  I need a way to cancel the execution of the task when I determine that there is a problem, but would prefer not to throw an exception.
 
From above:
      .AddAction((b) =>

     {

          if (b.HasError)

               throw b.Error;

 

          // Results indicate an entity with the same name already exists in the database.

          if (a.Results.Count() > 0)

               throw new Exception("Name is not unique");

     }

 
Looking for something like:
var task = AsyncSerialTask.Create()
      .*
      .AddAction((b) =>

     {

           // Throw here, because this actually is an Exception.

           if (b.HasError)

               throw b.Error;

 
           // Don't throw here, this isn't really an exception, but is a reason to stop processing.
           if (b.Results.Count() > 0)
           {
                // throw new Exception("Name is not unique");

               task.CancelExecution();

           }
      }
 
Again, not really a big issue, was just wondering if there was a preferred method of stopping processing of the remaining actions in a task, but it's looking to me like there's nothing 'built-in'.  It may all be moot anyway if 6.0.6 has something differernt/better in place for handling async tasks serially.
 
Thanks!
Ken
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 01-Nov-2010 at 1:16pm
Sorry ... the Continue flag was added after 6.0.4.
I wish there had been a Cancel command exactly as you suggest. I would move to add it ... except the entire class is deprecated.

What stops you from upgrading? I'm always curious about that :->
Back to Top
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Posted: 01-Nov-2010 at 3:21pm
Nothing is really stopping us from upgrading, it's really just a matter of timing.  I'll likely be the one doing the upgrade and have a few things that are higher priority at the moment.  Thanks again!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down