Wait for notification async call

Viewed 1181

I am trying to get the lync presence status of a given user. My code will talk to lync server 2010 using UCMA 4.0 in 64 bit env.

here is my code to wait for an async call to get the lync status.

private async void getNotifications(UserEndpoint endpoint, string useridSIP)
{
    _userEndpoint.PresenceServices.BeginPresenceQuery(
        new[] { useridSIP },
        new[] { "state" },
        null,
        (ar) => {
            Task<List<RemotePresentityNotification>> notificationFetch = _userEndpoint.PresenceServices.EndPresenceQuery(ar).ToList<RemotePresentityNotification>();
            List<RemotePresentityNotification> result = await notificationFetch;
            result.ForEach(x => {
                LyncUser user = new LyncUser();
                if (x.AggregatedPresenceState != null)
                {
                    user.Presence = x.AggregatedPresenceState.Availability.ToString();
                }
                else
                {
                    user.Presence = "Unknown";
                }
                user.UserName = x.PresentityUri.ToString();
                usersWithStatus.Add(user);
            });
        },
        null);
}

I am not sure how to wait till the List<RemotePresentityNotification> results are returned

Task<List<RemotePresentityNotification>> notificationFetch = _userEndpoint.PresenceServices.EndPresenceQuery(ar).ToList<RemotePresentityNotification>();
List<RemotePresentityNotification> result = await notificationFetch;

The entire source code.

using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Collaboration.Presence;
using Oobe.Bobs.Lync.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace xxxx.xxxx.xxxx
{
    public class OneTimePresence
    {
        private UCMASampleHelper _helper;
        private UserEndpoint _userEndpoint;
        public List<LyncUser> usersWithStatus = new List<LyncUser>();
        public LyncPresenceChecker _checker { get; set; }
        public OneTimePresence(string useridSIP, LyncPresenceChecker checker)
        {
            _checker = checker;
            _helper = new UCMASampleHelper();
            string endpoint = String.Format("OneTime Presence query for {0}", useridSIP);
            _userEndpoint = _helper.CreateEstablishedUserEndpoint(endpoint);
            getNotifications(_userEndpoint, useridSIP);
            _helper.ShutdownPlatform();

        }
        protected void EndgetNotification(object sender, RemotePresentitiesNotificationEventArgs e)
        {
            e.Notifications.ToList<RemotePresentityNotification>().ForEach(x =>
            {
                LyncUser user = new LyncUser();
                if (x.AggregatedPresenceState != null)
                {
                    user.Presence = x.AggregatedPresenceState.Availability.ToString();
                }
                else
                {
                    user.Presence = "Unknown";
                }
                user.UserName = x.PresentityUri.ToString();
                usersWithStatus.Add(user);
            });
            _checker.Clients.All.updateLyncUserPresence(usersWithStatus);

        }

        private void getNotifications(UserEndpoint endpoint, string useridSIP)
        {
            _userEndpoint.PresenceServices.BeginPresenceQuery(
                new[] { useridSIP },
                new[] { "state" },
                EndgetNotification,
                (ar) => {
                    ar.AsyncWaitHandle.WaitOne();
                    List<RemotePresentityNotification> result = _userEndpoint.PresenceServices.EndPresenceQuery(ar).ToList<RemotePresentityNotification>();

                    result.ForEach(x =>
                    {
                        LyncUser user = new LyncUser();
                        if (x.AggregatedPresenceState != null)
                        {
                            user.Presence = x.AggregatedPresenceState.Availability.ToString();
                        }
                        else
                        {
                            user.Presence = "Unknown";
                        }
                        user.UserName = x.PresentityUri.ToString();
                        usersWithStatus.Add(user);
                    });
                },
                null);
            if (usersWithStatus.Count > 0)
            {
                _checker.Clients.All.updateLyncUserPresence(usersWithStatus);
            }

        }


    }
}
1 Answers
Related