private CalendarEvent GetCallendarEvent(NotesDocument Current, int RepeatIndex)
        {
            CalendarEvent e = new CalendarEvent();
            try
            {

                e.Starts = (DateTime)((object[])Current.GetItemValue("StartDateTime"))[RepeatIndex];
                e.Ends = (DateTime)((object[])Current.GetItemValue("EndDateTime"))[RepeatIndex];
                e.Title = (string)((object[])Current.GetItemValue("Subject"))[0];
                e.Location = (string)((object[])Current.GetItemValue("Room"))[0] + " " +
                                (string)((object[])Current.GetItemValue("Location"))[0];
            }
            catch (Exception ex)
            {
               throw ex;
            }
            return e;
        }
 private void buttonStopSync_Click(object sender, RoutedEventArgs e)
 {
     currentCalendarEvent = null;
     currentlySyncing = false;
     tokenSource.Cancel();
     ResetUIControls();           
 }
        private async Task DoSync()
        {
            try
            {
                CalendarEvent ce;
                while (true)
                {
                    if (tokenSource.IsCancellationRequested)
                    {
                        break;
                    }

                    statusTextBlock.Text = "Retrieving Lotus Notes Calendar Entries...";
                    var notesTask = Task<CalendarEvent>.Factory.StartNew(() => notesCalendar.GetCurrentMeeting(notesPassword), tokenSource.Token);
                    await notesTask;
                    ce = notesTask.Result;

                    DateTime nextCheck = DateTime.Now.Add(TimeSpan.FromMinutes(updateFrequencyMinutes));
                    if (ce == null)
                    {
                        //check again in time specified                       
                        statusTextBlock.Text = "No Meetings Found, Checking Again at " + nextCheck.ToLocalTime();
                        await Task.Delay(updateFrequencyMinutes * 60 * 1000, tokenSource.Token);
                        continue;
                    }

                    if (ce.Starts > DateTime.Now)
                    {
                        TimeSpan startTimeDiff = ce.Starts.Subtract(DateTime.Now);
                        if (startTimeDiff.Minutes > updateFrequencyMinutes)
                        {
                            //wait then check again for changes
                            statusTextBlock.Text = "Meeting Found, But Checking Again at " + nextCheck.ToLocalTime().ToShortTimeString() + " In Case of Updates ("+ce.Title+")";
                            await Task.Delay(updateFrequencyMinutes * 60 * 1000, tokenSource.Token);
                            continue;
                        }
                                               
                        //wait the short amount of time and then update, although this isn't optimal
                        //for long refresh delays
                        DateTime shorterCheck = DateTime.Now.Add(startTimeDiff);
                        statusTextBlock.Text = "Meeting Found, Updating Status at " + shorterCheck.ToLocalTime().ToShortTimeString() + " (" + ce.Title + ")";
                        await Task.Delay(startTimeDiff);
                        continue;
                    
                    }
                    //else, the meeting is right now
                    currentCalendarEvent = ce;
                    SetLyncStatusWithGUIInfo();                    

                    //wait until the end of meeting to continue to check again
                    statusTextBlock.Text = "Status is Set, Waiting Until " + ce.Ends + " To Check Again";                    
                    await Task.Delay(ce.Ends - DateTime.Now, tokenSource.Token);

                    currentCalendarEvent = null;
                    
                    //Reset status after meeting
                    ResetLyncStatus();
                }
            }
            catch (TaskCanceledException)
            {
                statusTextBlock.Text = "Syncing Stopped";
                ResetUIControls(); 
            }
            catch (Exception ex)
            {
                statusTextBlock.Text = "ERROR: " + ex.Message;
                ResetUIControls(); 
            }
        }