Search code examples
c#wcf-data-services

How to access .net C# object which arrived in a delegate function?


I am running a remote synchronising routine on a local mobile. It will run in its own thread and therefore can take as long we want. I want to use the await pattern but I am unsure how to do this as my remote access uses a delegate function. Sorry, I"m a newby to C# so this is probably an easy question.

In my scenario I have the following code:

       public  static void testREADLiveConnection()
    {

        Uri tmaLiveDataRoot = new Uri("https://xxx.azurewebsites.net/xxx.svc/");
        TMLiveData.TMALiveData mLiveData = new TMLiveData.TMALiveData(tmaLiveDataRoot);
        mResult = null;


        DataServiceQuery<TMLiveData.JobType> query = (DataServiceQuery<TMLiveData.JobType>)mLiveData.JobTypes.Where(c => c.IsActive == true);
        mLiveData.Credentials = new System.Net.NetworkCredential("xx", "yy");

        mResult = "Trying to READ the data";
        try
        {
            query.BeginExecute(OnQueryComplete, query);
        }
        catch (Exception ex)
        {
            mResult = "Error on beginExecute: " + ex.Message;
        }


    }

    private static  void OnQueryComplete(IAsyncResult result)
    {

        DataServiceQuery<TMLiveData.JobType> query = (DataServiceQuery<TMLiveData.JobType>) result.AsyncState; 
        mResult = "Done!";
        try
        {
            foreach (TMLiveData.JobType jobType in query.EndExecute(result))
            {
                mResult += jobType.JobType1 + ",";
            }
        }catch (DataServiceClientException  ex)
        {
            mResult = "Error looping for items: (DataServiceClientException)" + ex.Message;
        }
        catch (DataServiceQueryException  ex2)
        {
            mResult = "Error looping for items: (DataServiceQueryException)" ;
        }
        catch (Exception ex3)
        {
            mResult = "Error looping for items: (general exception)" + ex3.Message;
        }

    }

The key point is that within the class I run a method, which has a delegate function which is called when the response comes.

Question: How can I use this class, from another class so that I wait for the response AND receive it.

ie. I want

testLSCon newRemoteObject;

listOfJobTypes = await newRemoteObject.testREADLiveConnection();

then do what i want with listOfJobTypes.

Thanks


Solution

  • I found the answer on another question - and it works perfectly well.

    The answer is found here

    The new code in the Try{} block of the first method becomes:

           try
            {
    
                //method 2 doing inline
                TaskFactory<IEnumerable<TMLiveData.JobType>> tf = new TaskFactory<IEnumerable<TMLiveData.JobType>>();
                IEnumerable < TMLiveData.JobType > jobTypes = await tf.FromAsync(query.BeginExecute(null, null),
                                           iar => query.EndExecute(iar));
                foreach (TMLiveData.JobType jobType in jobTypes )
                {
                    mResult += jobType.JobType1 + ",";
                }
    
    
                //method 1 using the onQueryComplete delegate function
                //query.BeginExecute(OnQueryComplete, query);
            }