[SalesForce] Examples of using queryAsync in SOAP API

I've been looking around online for examples for using queryAsync() in the SOAP api, but I can't seem to find any.

Can someone using Async calls using the SOAP WSDL api in C#?

Best Answer

The following is using the G4S .NET wrapper to establish the Salesforce Session, but the SalesforceServiceWrapper from the Binding property will be more or less the same as what you get from importing the SOAP WSDL.

    // Used to synchronise the threads in the test case.
    ManualResetEvent completion = null;

    [TestMethod]
    public void QueryAsync()
    {
        // Custom G4S code to establish a Salesforce session.
        SalesforceSession salesforceSession = codeRemoved; 

        // This should be the class that gets generated from the SOAP WSDL.
        SalesforceServiceWrapper salesforceServiceWrapper = salesforceSession.Binding;

        // Register for the async completion event.
        salesforceServiceWrapper.queryCompleted += 
            new queryCompletedEventHandler(Binding_queryCompleted);

        completion = new ManualResetEvent(false);
        // Note that you can optionally pass a UserState object that will be returned
        // in the completed EventArgs
        salesforceServiceWrapper.queryAsync("select Id from lead limit 1", "HelloWorld");
        completion.WaitOne();
    }

    void Binding_queryCompleted(object sender, queryCompletedEventArgs e)
    {
        // Check the EventArgs to see is the query was cancelled or there
        // was an exception.
        System.Diagnostics.Debug.WriteLine("Cancelled:{0} Error:{1} UserState:{2}", e.Cancelled, e.Error, e.UserState);

        // Get the results of the query
        QueryResult qr = e.Result;

        // Signal that the async operation is complete
        completion.Set();
    }