Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Peer to Peer Series Part 4: Resolving Peer Names Asynchronously

Download

Right click “Save as…”

Embed code for this video

Copy the code above to embed our video on your website/blog.

Close

Video format

Note: These selections will fall back to the next best format depending upon browser capability.

Close

In Part 4 of the Peer to Peer Series, I modify the Resolve program to find Peer Names using the asynchronous methods.  Below is a very simple example of using the ResolveAsync method:

PeerNameResolver resolver = new PeerNameResolver();
 
try
{
 resolver.ResolveCompleted += new EventHandler<ResolveCompletedEventArgs>(resolver_ResolveCompleted);
 resolver.ResolveAsync(new PeerName(classifier, PeerNameType.Secured), Guid.NewGuid());
}
catch (PeerToPeerException ex)
{
 // There are other standard exceptions you can also catch - see docs
 Console.WriteLine("PeerToPeer Excpetion: {0)", ex.Message);
}
 
static void resolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
{
 if (!e.Cancelled && e.Error == null && e.PeerNameRecordCollection != null)
 {
 records = e.PeerNameRecordCollection;
 DisplayResults();
 }
}

There is also a ResolveProgress event you can also wire up.  As the name implies, it will fire periodically to indicate the progress being made in resolving the Peer Name.  It was not overly useful when resolving a Peer Name that had a small number of results, but your mileage may vary.

The one thing that is also a “brute force” implementation in the demo code is how I pass a Guid.NewGuid() into the ResolveAsync method.  The Guid is being used as the UserState parameter in the method, and can actually be any Object. In a real implementation where you may have multiple ResovleAsync requests in-flight at any one time, you would want to implement a tracking system using this UserState parameter so  you could match up the corresponding ResolveCompleted event with the correct initiating request.  The ResolveCompleteEventArgs parameter contains the UserState used to initiate the request, so the matching is pretty straightforward.  I did not go the extra mile and implement that since the demo only fired a single ResolveAsync request.  You cannot pass in a null UserState object, so I went with a simple Guid for no particularly good reason.

Other documentation for PNRP:


Download the Demo Code

Tags:

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.