OpenTween.TweenMain.PostMessageAsyncInternal C# (CSharp) Method

PostMessageAsyncInternal() private method

private PostMessageAsyncInternal ( IProgress p, CancellationToken ct, PostingStatus status ) : Task
p IProgress
ct CancellationToken
status PostingStatus
return Task
        private async Task PostMessageAsyncInternal(IProgress<string> p, CancellationToken ct, PostingStatus status)
        {
            if (ct.IsCancellationRequested)
                return;

            if (!CheckAccountValid())
                throw new WebApiException("Auth error. Check your account");

            p.Report("Posting...");

            var errMsg = "";

            try
            {
                await Task.Run(async () =>
                {
                    if (status.mediaItems == null || status.mediaItems.Length == 0)
                    {
                        await this.tw.PostStatus(status.status, status.inReplyToId)
                            .ConfigureAwait(false);
                    }
                    else
                    {
                        var service = ImageSelector.GetService(status.imageService);
                        await service.PostStatusAsync(status.status, status.inReplyToId, status.mediaItems)
                            .ConfigureAwait(false);
                    }
                });

                p.Report(Properties.Resources.PostWorker_RunWorkerCompletedText4);
            }
            catch (WebApiException ex)
            {
                // 処理は中断せずエラーの表示のみ行う
                errMsg = $"Err:{ex.Message}(PostMessage)";
                p.Report(errMsg);
                this._myStatusError = true;
            }
            catch (UnauthorizedAccessException ex)
            {
                // アップロード対象のファイルが開けなかった場合など
                errMsg = $"Err:{ex.Message}(PostMessage)";
                p.Report(errMsg);
                this._myStatusError = true;
            }
            finally
            {
                // 使い終わった MediaItem は破棄する
                if (status.mediaItems != null)
                {
                    foreach (var disposableItem in status.mediaItems.OfType<IDisposable>())
                    {
                        disposableItem.Dispose();
                    }
                }
            }

            if (ct.IsCancellationRequested)
                return;

            if (!string.IsNullOrEmpty(errMsg) &&
                !errMsg.StartsWith("OK:", StringComparison.Ordinal) &&
                !errMsg.StartsWith("Warn:", StringComparison.Ordinal))
            {
                var message = string.Format(Properties.Resources.StatusUpdateFailed, errMsg, status.status);

                var ret = MessageBox.Show(
                    message,
                    "Failed to update status",
                    MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Question);

                if (ret == DialogResult.Retry)
                {
                    await this.PostMessageAsync(status);
                }
                else
                {
                    // 連投モードのときだけEnterイベントが起きないので強制的に背景色を戻す
                    if (this.ToolStripFocusLockMenuItem.Checked)
                        this.StatusText_Enter(this.StatusText, EventArgs.Empty);
                }
                return;
            }

            this._postTimestamps.Add(DateTime.Now);

            var oneHour = DateTime.Now - TimeSpan.FromHours(1);
            foreach (var i in MyCommon.CountDown(this._postTimestamps.Count - 1, 0))
            {
                if (this._postTimestamps[i] < oneHour)
                    this._postTimestamps.RemoveAt(i);
            }

            if (!this.HashMgr.IsPermanent && !string.IsNullOrEmpty(this.HashMgr.UseHash))
            {
                this.HashMgr.ClearHashtag();
                this.HashStripSplitButton.Text = "#[-]";
                this.HashToggleMenuItem.Checked = false;
                this.HashToggleToolStripMenuItem.Checked = false;
            }

            this.SetMainWindowTitle();

            if (this._cfgCommon.PostAndGet)
            {
                if (this.tw.UserStreamActive)
                    this.RefreshTimeline();
                else
                    await this.GetHomeTimelineAsync();
            }
        }
TweenMain