ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

winform 使用task不卡界面的2种方式

2020-06-30 13:04:57  阅读:339  来源: 互联网

标签:task 不卡 System button2 button1 lblMsg new Drawing winform


 

方法1:

            button1.Enabled = false;
            TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();
            Task t = Task.Factory.StartNew(() =>
             {
                 for (int i = 0; i < 5; i++)
                 {
                     Console.WriteLine("任务1: " + DateTime.Now.ToString("ss.fff"));
                     Task.Delay(1000).Wait();
                 }
                 return 1;
             }).ContinueWith(m =>
             {
                 button1.Enabled = true;
                 Console.WriteLine("任务1:结束");
             }, ui);

方法2:

            button2.Enabled = false;
            TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();
            Task t = Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("任务2: " + DateTime.Now.ToString("ss.fff"));
                    Task.Delay(1000).Wait();
                }
                return 1;
            }).ContinueWith(m =>
            {
                this.Invoke(new MethodInvoker(() =>
                {
                    button2.Enabled = true;
                    Console.WriteLine("任务2:结束");
                }));
            });

 

 

using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();
            Task t = Task.Factory.StartNew(() =>
             {
                 for (int i = 0; i < 5; i++)
                 {
                     Console.WriteLine("任务1: " + DateTime.Now.ToString("ss.fff"));
                     Task.Delay(1000).Wait();
                 }
                 return 1;
             }).ContinueWith(m =>
             {
                 button1.Enabled = true;
                 Console.WriteLine("任务1:结束");
             }, ui);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();
            Task t = Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("任务2: " + DateTime.Now.ToString("ss.fff"));
                    Task.Delay(1000).Wait();
                }
                return 1;
            }).ContinueWith(m =>
            {
                ButtonEnabled();
            });
        }

        private void ButtonEnabled()
        {
            this.Invoke(new MethodInvoker(() =>
            {
                button2.Enabled = true;
                Console.WriteLine("任务2:结束");
            }));
        }

        private int i = 0;
        private void btnOperate_Click(object sender, EventArgs e)
        {
            lblMsg.Text = i++.ToString();
        }
    }
}
View Code

 

namespace WindowsFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.btnOperate = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lblMsg = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(161, 51);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "开始";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // btnOperate
            // 
            this.btnOperate.Location = new System.Drawing.Point(312, 62);
            this.btnOperate.Name = "btnOperate";
            this.btnOperate.Size = new System.Drawing.Size(150, 99);
            this.btnOperate.TabIndex = 0;
            this.btnOperate.Text = "点开始后,再点我不阻塞";
            this.btnOperate.UseVisualStyleBackColor = true;
            this.btnOperate.Click += new System.EventHandler(this.btnOperate_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(161, 127);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 0;
            this.button2.Text = "开始";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(91, 62);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(47, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "方法1:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(91, 132);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(47, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "方法2:";
            // 
            // lblMsg
            // 
            this.lblMsg.AutoSize = true;
            this.lblMsg.BackColor = System.Drawing.Color.Transparent;
            this.lblMsg.Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblMsg.ForeColor = System.Drawing.Color.Tomato;
            this.lblMsg.Location = new System.Drawing.Point(352, 26);
            this.lblMsg.Name = "lblMsg";
            this.lblMsg.Size = new System.Drawing.Size(65, 22);
            this.lblMsg.TabIndex = 1;
            this.lblMsg.Text = "-----";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(510, 254);
            this.Controls.Add(this.lblMsg);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.btnOperate);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button btnOperate;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label lblMsg;
    }
}
View Code

 

标签:task,不卡,System,button2,button1,lblMsg,new,Drawing,winform
来源: https://www.cnblogs.com/slwangzi/p/13213086.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有