Forms/T_SendProgress.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChatworkBulkSender.Utils;

namespace ChatworkBulkSender.Forms
{
    public partial class T_SendProgress : Form
    {
        // キャンセル用のソースを外部からセットできるようにする
        public CancellationTokenSource CancellationTokenSource { get; set; }

        public T_SendProgress()
        {
            InitializeComponent();

            FormBorderStyle = FormBorderStyle.FixedToolWindow;

            // プログレスバー初期化
            progressBar1.Minimum = 0;
        }

        /// <summary>
        /// 最大値をセット
        /// </summary>
        public void SetMaximum(int max)
        {
            progressBar1.Maximum = max;
        }

        /// <summary>
        /// 進捗を更新
        /// </summary>
        public void ReportProgress(int current)
        {
            progressBar1.Value = current;
            if (current == progressBar1.Maximum)
            {
                lblStatus.Text = $"{current} / {progressBar1.Maximum} 件 送信完了";
            }
            else
            {
                lblStatus.Text = $"{current} / {progressBar1.Maximum} 件 送信中...";
            }
            Application.DoEvents();  // UI 更新を即座に反映
        }

        private void btnEmergencyStop_Click(object sender, EventArgs e)
        {
            // 最終確認
            var result = MessageBoxUtil.ShowYesNo(MessageBoxUtil.ASK_003);
            if (result != DialogResult.Yes) return;

            // 強制終了
            CancellationTokenSource?.Cancel();
        }
    }
}