locked
abstract methods with sync/async implementation

    Question

  • Hi,

    I am new to the asynchronous programming pattern in windows runtime. I have an abstract base class which have an abstract method GetData() with some return value, and the actual implementation may take some time, may need network or database access, but maybe also do nothing just return null.

    how should I write the abstract method signature and how to do async/sync work in the implementation? suppose the structure is like this:

    public abstract class Source {
        public abstract byte[] GetData(Param p);
    
        public /*async?*/ virtual void Process() {
            byte[] data = /*await?*/ GetData(new Param(...));
            // how others call the above implementation?
        }
    }
    
    public class NetworkSource : Source {
        public /*async?*/ override byte[] GetData(Param p) {
            // await HttpClient.GetAsync() ?
        }
    }
    
    public class DummySource : Source {
        public override byte[] GetData(Param p) {
            // do nothing
            return new byte[] { };
        }
    }

    Also, if the implementation does not call any async methods outside but itself requires a lot of computation time, how can I make it async?

    Thanks!

    Monday, May 05, 2014 2:34 PM

Answers

  • See the docs I linked for details. You may have to drill deeper through those links depending on how much detail you want.

    In essence, set the return type to Task< your type> and then return your you're from the function.

    I'm on my phone so I'll let you read the docs rather than trying to type more details.

    Monday, May 05, 2014 3:12 PM
    Owner
  • you can use Task.FromResult for methods that directly return a result

    public class DummySource : Source {
        public override Task<byte[]> GetData(Param p) {
            // do nothing
            return Task.FromResult(new byte[]);
        }
        }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, May 06, 2014 9:41 AM

All replies

  • They need to return a Task to be awaited. By convention the function name should end in Async. Being abstract won't make any difference here.

    The function implementation will need to make sure lengthy operations don't hang. This may be done by calling other Async functions (e.g. NetworkSource's call to HttpClient.GetAsync), by moving them to a ThreadPool, etc.

    See Asynchronous programming in .NET (Windows Runtime apps using C#/VB/C++ and XAML)

    --Rob

    Monday, May 05, 2014 2:43 PM
    Owner
  • So the abstract signature shoud be returning a Task to let the implementation be awaited?

    What if the implementation do very little things such as returning null immediately, how to make this as a Task?

    Monday, May 05, 2014 2:57 PM
  • See the docs I linked for details. You may have to drill deeper through those links depending on how much detail you want.

    In essence, set the return type to Task< your type> and then return your you're from the function.

    I'm on my phone so I'll let you read the docs rather than trying to type more details.

    Monday, May 05, 2014 3:12 PM
    Owner
  • you can use Task.FromResult for methods that directly return a result

    public class DummySource : Source {
        public override Task<byte[]> GetData(Param p) {
            // do nothing
            return Task.FromResult(new byte[]);
        }
        }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, May 06, 2014 9:41 AM