internal void PrepareToRaiseCreateContactCompletedEvent(AsyncOperation asyncOP, CreateContactCompletedEventArgs args)
 {
     asyncOP.PostOperationCompleted(OnCreateContactCompletedDelegate, args);
 }
        //
        // Used to create a contact async'ally
        //
        private void CreateContactAsyncHelper(object state)
        {
            Exception ex = null;
            PeerContact peerContact = null;
            CreateContactAsyncState createAsyncState = state as CreateContactAsyncState;
            PeerNearMe peerNearMe = createAsyncState.PeerNearMe;
            object userToken = createAsyncState.UserToken;

            //
            // Call the [....] version of createcontact
            //
            try{
                peerContact = CreateContact(peerNearMe);
            }
            catch (ObjectDisposedException e){
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "CreateContactAsyncHelper caught error {0}", e.Message);
                ex = e;
            }
            catch (PeerToPeerException e){
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "CreateContactAsyncHelper caught error {0}", e.Message);
                ex = e;
            }

            CreateContactCompletedEventArgs createContactCompletedArgs;
            if (ex == null){
                createContactCompletedArgs = new CreateContactCompletedEventArgs(peerContact, null, false, userToken);
            }
            else{
                createContactCompletedArgs = new CreateContactCompletedEventArgs(peerContact, ex, false, userToken);
            }

            PrepareToRaiseCreateContactCompletedEvent(m_createContactAsyncList[userToken], createContactCompletedArgs);
        }
 void OnCreateContactCompleted(CreateContactCompletedEventArgs e)
 {
     EventHandler<CreateContactCompletedEventArgs> handlerCopy = m_createContactCompleted;
     if (handlerCopy != null){
         handlerCopy(this, e);
         Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Fired the create contact completed event callback.");
     }
 }
 void ContactManager_CreateContactCompleted(object sender, CreateContactCompletedEventArgs e)
 {
     switch (e.UserState.ToString())
     {
         case "Setup":
             try
             {
                 PeerContact existContact = PeerCollaboration.ContactManager.GetContact(e.PeerContact.PeerName);
                 PeerCollaboration.ContactManager.UpdateContact(e.PeerContact);
             }
             catch (PeerToPeerException ex)
             {
                 AddLog("ContactManager_CreateContactCompleted:" + ex.Message, LogType.System);
                 PeerCollaboration.ContactManager.AddContact(e.PeerContact);
             }
             e.PeerContact.SubscribeCompleted += peerContact_SubscribeCompleted;
             e.PeerContact.SubscribeAsync(peerObject);
             AddLog("[Setup]ContactManager_CreateContactCompleted", LogType.System);
             break;
         case "Added":
             try
             {
                 PeerContact existContact = PeerCollaboration.ContactManager.GetContact(e.PeerContact.PeerName);
                 PeerCollaboration.ContactManager.UpdateContact(e.PeerContact);
             }
             catch (PeerToPeerException ex)
             {
                 AddLog("ContactManager_CreateContactCompleted:" + ex.Message, LogType.System);
                 PeerCollaboration.ContactManager.AddContact(e.PeerContact);
             }
             e.PeerContact.SubscribeCompleted += peerContact_SubscribeCompleted;
             e.PeerContact.SubscribeAsync(peerObject);
             AddLog("[Added]ContactManager_CreateContactCompleted", LogType.System);
             break;
         case "Deleted":
             e.PeerContact.SubscribeCompleted -= peerContact_SubscribeCompleted;
             PeerCollaboration.ContactManager.DeleteContact(e.PeerContact);
             AddLog("[Deleted]ContactManager_CreateContactCompleted", LogType.System);
             break;
     }
 }