Freigeben über


Using the SharePoint 2010 Managed Client Object Model – Asynchronous Processing

This is a clipboard friendly version of example #16, Asynchronous Processing, from Using the SharePoint 2010 Managed Client Object Model.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;

class Program
{
static void Main(string[] args)
{
AsynchronousAccess asynchronousAccess = new AsynchronousAccess();
asynchronousAccess.Run();
Console.WriteLine("Before exiting Main");
Console.WriteLine();
Console.WriteLine("In a real application, the application can");
Console.WriteLine("continue to be responsive to the user.");
Console.WriteLine();
Console.ReadKey();
}
}

class AsynchronousAccess
{
delegate void AsynchronousDelegate();

public void Run()
{
Console.WriteLine("About to start a query that will take a long time.");
Console.WriteLine();
ClientContext clientContext =
new ClientContext("https://intranet.contoso.com");
ListCollection lists = clientContext.Web.Lists;
IEnumerable<List> newListCollection = clientContext.LoadQuery(
lists.Include(
list => list.Title));
AsynchronousDelegate executeQueryAsynchronously =
new AsynchronousDelegate(clientContext.ExecuteQuery);
executeQueryAsynchronously.BeginInvoke(
arg =>
{
Console.WriteLine("Long running query has completed.");
foreach (List list in newListCollection)
Console.WriteLine("Title: {0}", list.Title);
}, null);
}
}

Following is the same example using fully qualified names for better discoverability.

using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;

class Program
{
static void Main(string[] args)
{
AsynchronousAccess asynchronousAccess = new AsynchronousAccess();
asynchronousAccess.Run();
Console.WriteLine("Before exiting Main");
Console.WriteLine();
Console.WriteLine("In a real application, the application can");
Console.WriteLine("continue to be responsive to the user.");
Console.WriteLine();
Console.ReadKey();
}
}

class AsynchronousAccess
{
delegate void AsynchronousDelegate();

public void Run()
{
Console.WriteLine("About to start a query that will take a long time.");
Console.WriteLine();
Microsoft.SharePoint.Client.ClientContext clientContext =
new Microsoft.SharePoint.Client.ClientContext("https://intranet.contoso.com");
Microsoft.SharePoint.Client.ListCollection lists = clientContext.Web.Lists;
IEnumerable<Microsoft.SharePoint.Client.List> newListCollection = clientContext.LoadQuery(
lists.Include(
list => list.Title));
AsynchronousDelegate executeQueryAsynchronously =
new AsynchronousDelegate(clientContext.ExecuteQuery);
executeQueryAsynchronously.BeginInvoke(
arg =>
{
Console.WriteLine("Long running query has completed.");
foreach (Microsoft.SharePoint.Client.List list in newListCollection)
Console.WriteLine("Title: {0}", list.Title);
}, null);
}
}