Wrapping a callback-based class to an async one

Viewed 680

I'm referencing a class that performs various demanding operations using callbacks rather than async operations. For example:

class CallbackClass : SomeSdkCallbacks
{
    public void RequestData()
    {
        // We call this to request some data.
        // After some time, this will trigger OnDataReturned to be called
    }

    public void OnDataReturned(DataObject data)
    {
        // This will be called automatically with returned data via SomeSdkCallbacks
    }
}

There are many instances of this type of structure throughout this class.

What I'm trying to do is create a wrapper class around this one that simplifies it's usage. The structure that I'm going for at the moment is:

class MyWrapperClass
{
    CallbackClass CallbackClass;

    public MyWrapperClass()
    {
        this.CallbackClass = new CallbackClass();
    }

    public DataObject GetData()
    {
        this.CallbackClass.RequestData();

        // Somehow wait until this.CallbackClass.OnDataReturned is called?

        // Somehow get the data that would be passed in to this.CallbackClass.OnDataReturned()???
    }
}

What is the best method of wrapping up a 'callback' architecture into a single asynchronous method?

Edit: To clarify, ideally I would like to package this up into a single method that can return the data in a single request. See the MyWrapperClass.GetData() method in my example for my ideal structure.

Edit 2: I'm aware that this desired architecture is bad form. Unfortunately that is the requested structure that has been asked of me.

2 Answers
Related