private async Task ZImageLoadAsync(int pQueryBodyStructureDetailEntryNumber, cSinglePartBody pPart, int pSize) { frmProgress lProgress = null; MemoryStream lStream = null; try { cBodyFetchConfiguration lConfiguration; if (mProgressBar) { lProgress = new frmProgress("image " + pPart.Section.Part + " [" + pSize + " bytes]", pSize); lProgress.ShowAndFocus(this); lConfiguration = new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment); mImageLoadProgress = lProgress; // so it can be cancelled from code } else { lConfiguration = null; } lStream = new MemoryStream(); await mMessage.FetchAsync(pPart, lStream, lConfiguration); if (IsDisposed || pQueryBodyStructureDetailEntryNumber != mQueryBodyStructureDetailEntryNumber) { throw new Exception(); } mImageStream = lStream; // so it can be closed } catch (Exception e) { if (lStream != null) { lStream.Dispose(); } if (IsDisposed || pQueryBodyStructureDetailEntryNumber != mQueryBodyStructureDetailEntryNumber) { return; } rtxDecoded.Text = e.ToString(); throw; } finally { if (lProgress != null) { lProgress.Complete(); } } }
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}"); } } } }
private async void mCurrentMailbox_MessageDelivery(object sender, cMessageDeliveryEventArgs e) { // cautions; // the event could be for the previously selected mailbox // this could arrive during the query // therefore some of the messages could be on the grid already // we could be processing one of these when the next one arrives if (e.MessageHandles[0].MessageCache != mCurrentMessageCache) { return; } if (mFilter != null) { return; } var lBindingSource = dgvMessages.DataSource as BindingSource; if (lBindingSource == null) { return; } frmProgress lProgress = null; List <cMessage> lMessages; try { if (mProgressBar) { lProgress = new frmProgress("loading new messages", e.MessageHandles.Count); lProgress.ShowAndFocus(this); var lConfiguration = new cCacheItemFetchConfiguration(lProgress.CancellationToken, lProgress.Increment); ZMessagesLoadingAdd(lProgress); // so it can be cancelled from code lMessages = await mCurrentMailbox.MessagesAsync(e.MessageHandles, null, lConfiguration); } else { lMessages = await mCurrentMailbox.MessagesAsync(e.MessageHandles); } } catch (OperationCanceledException) { return; } catch (Exception ex) { if (!IsDisposed) { MessageBox.Show(this, $"a problem occurred: {ex}"); } return; } finally { if (lProgress != null) { lProgress.Complete(); } } // check that while getting the messages we haven't been closed or the mailbox changed if (IsDisposed || e.MessageHandles[0].MessageCache != mCurrentMessageCache) { return; } // load the grid with data ZAddMessagesToGrid(lMessages); }