Archived

This forum has been archived. Please start a new discussion on GitHub.

old amd callback object disappare in 3.7.2

ggslayer
edited July 2019 in Help Center

in 3.7.2, c# server side slice-generated code changed, from older "amd parameter" to the new task mode, I can't find the amd parameter from the parameter list in method.

in my usage expirence of amd, it is useful like this:

void (...amd  callback, int para1, int para2)
{
    // do something must sync
    // ex:  write to db

    // give client response first
    callback.ice_response();

    // do something async
    // ex: send a mq message or something longtime job
}

now, with the new async mode in c#, how this can be done?

Tagged:

Comments

  • xdm
    xdm La Coruña, Spain
    edited July 2019
    public override Task<string> opStringAsync(Ice.Current current)
    {
        // do something must sync
        // ex:  write to db
        return Task<string>.Run(() =>
            {
                   // do something async
                    // ex: send a mq message or something longtime job
                    return "hello world!";
             });
    }
    

    Or simpler using async/await

    public override async Task<string> opStringAsync(Ice.Current current)
    {
         // do something must sync
         // ex:  write to db
    
         // await the async operation
         await xxxAsync();
         return "hello world!";
    }
    

    See AMD Tasks in C#

  • The answer not my question, the ice documents can describe my question:

    In doc3.6's "amd":
    An alternate use case for AMD is an operation that requires further processing after completing the client's request. In order to minimize the client's delay, the operation returns the results while still in the dispatch thread, and then continues using the dispatch thread for additional work.

    But in doc3.7 , this is disappear. how to implement the affect in 3.6's amd now?

  • xdm
    xdm La Coruña, Spain
    edited August 2019

    With the new async API you cannot do exactly the same, that is why this was removed from the doc. You can do something similar for example:

    var t = Task.FromResult("hello world!");
    t.ContinueWith(previous =>
    {
        // do something async
        // ex: send a mq message or something longtime job
    });
    return t;
    
  • This resolve my question partly, like this:

    lock locker;

    locker.lock();      // this lock cross async proc, is invalid.
    var t = Task.FromResult("hello world!");
    t.ContinueWith(previous =>
    {
        //do long job1
        ....
    
        locker.unlock().
    
        //do long job2
        ...
    });
    return t;
    

    In this situation, all or part of async code could in the lock, but I can't use the lock in async

  • xdm
    xdm La Coruña, Spain

    Hi,

    C# doesn't allow the locks to be used with async code, you can implement something like this using SemaphoreSlim.WaitAsync