public void session_SessionFound(IAsyncResult result)
{
// All sessions found
AvailableNetworkSessionCollection availableSessions;
// The session we will join
AvailableNetworkSession availableSession = null;
if (AsyncSessionFind.IsCompleted)
{
Console.WriteLine("AsyncSessionFind.IsCompleted");
availableSessions = NetworkSession.EndFind(result);
// Look for a session with available gamer slots
foreach (AvailableNetworkSession curSession in availableSessions)
{
Console.WriteLine("foreach");
int TotalSessionSlots = curSession.OpenPublicGamerSlots + curSession.OpenPrivateGamerSlots;
if (TotalSessionSlots > curSession.CurrentGamerCount)
{
Console.WriteLine("TotalSessionSlots");
availableSession = curSession;
}
}
// If a session was found, connect to it
if (availableSession != null)
{
message = "Found an available session at host " + availableSession.HostGamertag;
session = NetworkSession.Join(availableSession);
}
else
message = "No sessions found.";
// Reset the session finding result
AsyncSessionFind = null;
}
}