Print Page | Close Window

AsyncSerialTask

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1501
Printed Date: 16-Apr-2025 at 7:31am


Topic: AsyncSerialTask
Posted By: tj62
Subject: AsyncSerialTask
Date Posted: 30-Sep-2009 at 7:53am

Hi,

I'm trying to use AsyncSerialTask with the  AddAsyncInvokeServerMethod(). However the declaration of this metod that the Intellisens shows looks like chinees to me. I could not find any DevForce documentation for this nor any samples. The documentation includes one sample for AsyncSerialTask with limited explanation...hard to understand.
 
Can anyone explain for me how to use AssyncSerialTask.Create().AddAsyncInvokServerMethod()
 
Here is the serial sequence I'm trying to move to AsyncSerialTask:
 
  private void LoadData(string userID) {
   Guid myToken = Guid.NewGuid();
   object[] param = {userID};
   m_mgr.InvokeServerMethodAsync("C2NetDomainModel.EntCustomer,C2NetDomainModel",
            "GetCustomerIDs",
            GotCustomerIDs, myToken, userID);
  }
  public void GotCustomerIDs( InvokeServerMethodEventArgs args )
  {
   if( args.Error != null )
    WriteMessage( args.Error.Message );
   else
   {
    int[] customerIDs = ((C2NetDomainModel.IntIDs)(args.Result)).p_ids;
    var customerQ = m_mgr.EntCustomerSet
           .OrderBy( c => c.p_name )
           .WhereIn( c => c.p_customerID , customerIDs );
    m_mgr.ExecuteQueryAsync( (IEntityQuery<EntCustomer>)customerQ, GotCustomers , null );
   }
  }
  private void GotCustomers(EntityFetchedEventArgs args)
  {
   if (args.Error != null) {
    WriteMessage(args.Error.Message);
   }
   else {
    //foreach( EntCustomer c in (System.Linq.IQueryable)args.Result )
    foreach( EntCustomer c in (IEnumerable<EntCustomer>)args.Result )
    {
     _customers.Add(c);
    }
    // Set CurrentItem to first Customer so form will flesh out
    if (_customers.Count > 0) {
     _customerDataForm.CurrentItem = _customers[0];
    }
    ReportFetchCount(args.Result, "Customer");
   }
  }

Can anyone tell me how to change this to a AsyncSerialTask?
 



Replies:
Posted By: kimj
Date Posted: 30-Sep-2009 at 2:22pm
The syntax of the AsyncSerialTask is pretty abstruse.  Here's one way to implement this -

 private void LoadData(string userID) {
   //Guid myToken = Guid.NewGuid();
   //object[] param = {userID};
   AsyncSerialTask.Create<string>()
        .AddAsyncInvokeServerMethod<object, object, InvokeServerMethodEventArgs>(m_mgr,
  "C2NetDomainModel.EntCustomer,C2NetDomainModel", "GetCustomerIDs",
   inputArg=> new object[] {inputArg})
        .AddAsyncQuery(args => GotCustomerIds(args))
        .AddExceptionHandler(exArgs => WriteMessage(exArgs.Exception.Message))
        .Execute(userID, (completionArgs) => GotCustomers(completionArgs.Result));
  }
  public IEntityQuery<EntCustomer> GotCustomerIDs( InvokeServerMethodEventArgs args )
  {
    int[] customerIDs = ((C2NetDomainModel.IntIDs)(args.Result)).p_ids;
    var customerQ = m_mgr.EntCustomerSet
           .OrderBy( c => c.p_name )
           .WhereIn( c => c.p_customerID , customerIDs );
    return customerQ;
  }
  private void GotCustomers(EntityFetchedEventArgs<EntCustomer> args)
  {
    foreach( EntCustomer c in args.Result )
    {
     _customers.Add(c);
    }
    // Set CurrentItem to first Customer so form will flesh out
    if (_customers.Count > 0) {
     _customerDataForm.CurrentItem = _customers[0];
    }
    ReportFetchCount(args.Result, "Customer");
  }
 
Things to note -
1) When you create the task you can specify the type of the input argument, in this case a string.
2) A few of the overloads for AddAsyncInvokeServerMethod take a lambda as a final argument.  This is what we're using here: it takes a string (the input to the task) and returns an object array containing that string.
3) AddAsyncInvokeServerMethod defines T0, T1 and T2 and I admit I don't know why there are 3 type arguments instead of two.  But anyway, T0 and T1 should be the input type (and can be 'object' if no input is provided, and T2 is 'InvokeServerMethodArgs'. 
4) Exceptions are handled a bit differently with an AsyncSerialTask, and you should add an exception handler to the task to trap any errors.
5) GotCustomerIDs takes the InvokeServerMethodEventArgs and builds a query, which it then returns as the async query to be run next.
6) task.Execute accepts an input argument, in this case the string userID.  This is passed to the first action in the task.
7) At completion, the results of the last action are available in the Result property.  This is passed to the GotCustomers method.



Print Page | Close Window