Example #1
0
        private async void ZQueryMessagesAsync()
        {
            // defend against re-entry during awaits
            int lQueryMessagesAsyncEntryNumber = ++mQueryMessagesAsyncEntryNumber;

            // terminate any outstanding message loading
            ZMessagesLoadingClose();

            dgvMessages.Enabled    = false;
            dgvMessages.DataSource = new BindingSource();

            if (mCurrentMailbox == null)
            {
                return;
            }

            // cautions;
            //  message delivery could arrive during the query
            //   therefore some of the messages could be on the grid already

            frmProgress     lProgress = null;
            List <cMessage> lMessages;

            try
            {
                if (mProgressBar)
                {
                    lProgress = new frmProgress("loading messages");
                    lProgress.ShowAndFocus(this);
                    ZMessagesLoadingAdd(lProgress); // so it can be cancelled from code
                }

                if (mCurrentMailbox.MessageCount > mMaxMessages)
                {
                    cMessageFetchConfiguration lMFConfiguration;

                    if (lProgress == null)
                    {
                        lMFConfiguration = null;
                    }
                    else
                    {
                        lMFConfiguration = new cMessageFetchConfiguration(lProgress.CancellationToken, null, null);  // the setcount and progress will never be used as we aren't asked for items to be cached
                    }
                    // first get the messages, sorted, but don't get the requested properties yet (as this would be wasteful if we are about to trim the set of messages we are going to display)
                    lMessages = await mCurrentMailbox.MessagesAsync(mFilter, mOverrideSort, cMessageCacheItems.Empty, lMFConfiguration);

                    if (IsDisposed || lQueryMessagesAsyncEntryNumber != mQueryMessagesAsyncEntryNumber)
                    {
                        return;
                    }

                    // remove any excess messages (the filter may have removed enough or the mailbox may have changed in the meantime)
                    if (lMessages.Count > mMaxMessages)
                    {
                        lMessages.RemoveRange(mMaxMessages, lMessages.Count - mMaxMessages);
                    }

                    cCacheItemFetchConfiguration lCIFConfiguration;

                    if (lProgress == null)
                    {
                        lCIFConfiguration = null;
                    }
                    else
                    {
                        lProgress.SetCount(lMessages.Count);
                        lCIFConfiguration = new cCacheItemFetchConfiguration(lProgress.CancellationToken, lProgress.Increment);
                    }

                    // get any missing cache items
                    await mClient.FetchAsync(lMessages, mClient.DefaultMessageCacheItems, lCIFConfiguration);
                }
                else if (mFilter != null || mOverrideSort != null || lProgress != null)
                {
                    cMessageFetchConfiguration lConfiguration;

                    if (lProgress == null)
                    {
                        lConfiguration = null;
                    }
                    else
                    {
                        lConfiguration = new cMessageFetchConfiguration(lProgress.CancellationToken, lProgress.SetCount, lProgress.Increment);
                    }

                    lMessages = await mCurrentMailbox.MessagesAsync(mFilter, mOverrideSort, null, lConfiguration); // demonstrate the full API (note that we could have specified non default message properties if required)
                }
                else
                {
                    lMessages = await mCurrentMailbox.MessagesAsync();  // show that getting the full set of messages in a mailbox is trivial if no restrictions are required and the defaults are set correctly
                }
            }

            /* this is commented out as it hides problems in the gating code
             * catch (OperationCanceledException e)
             * {
             *  if (lProgress != null && lProgress.CancellationToken.IsCancellationRequested) return; // ignore the cancellation if we cancelled it
             *  if (!IsDisposed) MessageBox.Show(this, $"a problem occurred: {e}");
             *  return;
             * } */
            catch (Exception e)
            {
                if (!IsDisposed)
                {
                    MessageBox.Show(this, $"a problem occurred: {e}");
                }
                return;
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }

            // check that while getting the messages we haven't been closed or re-entered
            if (IsDisposed || lQueryMessagesAsyncEntryNumber != mQueryMessagesAsyncEntryNumber)
            {
                return;
            }

            // load the grid with data
            ZAddMessagesToGrid(lMessages);

            // enable
            dgvMessages.Enabled = true;

            // initialise unseen count
            if (mCurrentMailbox.UnseenUnknownCount > 0 && mTrackUnseen)
            {
                try { await mCurrentMailbox.SetUnseenCountAsync(); }
                catch (Exception ex)
                {
                    if (!IsDisposed)
                    {
                        MessageBox.Show(this, $"an error occurred while setting unseen: {ex}");
                    }
                }
            }
        }