task 子线程添加数据到主线程 winfrom

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;

namespace 导出dbf文件
{
    public partial class Form1 : Form
    {
        #region 公共变量
        static CancellationTokenSource cts = new CancellationTokenSource();
        CancellationToken ct = cts.Token;
        ManualResetEvent resetEvent = new ManualResetEvent(true);
        List<Task> listTask = new List<Task>();
        #endregion
        public Form1()
        {
            InitializeComponent();
            pro_Task.Visible = false;
        }
        /// <summary>
        /// 开始导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_imp_Click(object sender, EventArgs e)
        {
            #region  检查是否有任务 保证一次只执行一个任务
            if (listTask.Count > 0)
            {
                if (listTask.Where(x => x.IsCompleted == false).Count() > 0)
                {
                    MessageBox.Show($"请完成当前任务,或者取消当前任务");
                    return;
                }
            }
            pro_Task.Value = 0;
            pro_Task.Visible = true;
            pro_Task.Maximum = 999;
            #endregion
            #region 立即执行
            listTask.Add(Task.Run(() =>
            {
             
                for (int i = 0; i < 1000; i++)
                {
                    if (!cts.IsCancellationRequested)
                    {
                        resetEvent.WaitOne();
                        pro_Task.Invoke(new Action(() => { pro_Task.Value = i; }));
                        textBox1.Invoke(new Action(() =>
                        {
                            textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "_" + i + System.Environment.NewLine);
                        }));
                    }
                    else
                    {
                        return;
                    }
                }
            }, ct)
            .ContinueWith(x =>
            {
                if (x.IsCanceled || x.IsFaulted || cts.IsCancellationRequested)
                {
                    MessageBox.Show($"任务被取消");
                }
                else
                {
                    MessageBox.Show($"任务已完成");
                }
                cts = new CancellationTokenSource();
                ct = cts.Token;
                listTask.Clear();
                pro_Task.Invoke(new Action(() => { pro_Task.Visible = false; }));
            }));
            #endregion
            #region 延迟执行
            //listTask.Add(new Task(() =>
            //{
            //    for (int i = 0; i < 10000; i++)
            //    {
            //        if (!cts.IsCancellationRequested)
            //        {
            //            textBox1.Invoke(new Action(() => textBox1.AppendText($"循环已执行到{i}{Environment.NewLine}")));
            //        }
            //        else
            //        {
            //            return;
            //        }
            //    }
            //}, ct));
            //listTask[0].Start();
            //listTask[0].ContinueWith(x => {
            //    if (x.IsCanceled || x.IsFaulted|| cts.IsCancellationRequested)
            //    {
            //        MessageBox.Show($"任务被取消");
            //    }
            //    else
            //    {
            //        MessageBox.Show($"任务已完成");
            //    }
            //    cts = new CancellationTokenSource();
            //    ct = cts.Token;
            //    listTask.Clear();
            //});
            #endregion
        }
        /// <summary>
        /// 取消导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_ImpCancel_Click(object sender, EventArgs e)
        {
            ChecklistTask(listTask.Count, null, resetEvent.Set);
            ChecklistTask(listTask.Count, cts.Cancel);
        }
        /// <summary>
        /// 线程停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_ImpStop_Click(object sender, EventArgs e)
        {
            ChecklistTask(listTask.Count,null,resetEvent.Reset);
        }
        /// <summary>
        /// 线程继续
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_ImpCon_Click(object sender, EventArgs e)
        {
            ChecklistTask(listTask.Count, null,resetEvent.Set);
        }
        /// <summary>
        /// 检查是否有任务并执行方法
        /// </summary>
        /// <param name="listTask"></param>
        /// <param name="action"></param>
        public void ChecklistTask(int listTask, Action action =null, Func<bool> Func=null)
        {
            if (listTask > 0)
            {
                if (action != null)
                {
                    action();
                }
                else if (Func != null)
                {
                    Func();
                }
                else
                {

                }
            }
        }

    }
}

 

posted @ 2022-04-17 01:02  Sumi-eCrow  阅读(102)  评论(0编辑  收藏  举报