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


    方法1:

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

    方法2:

                button2.Enabled = false;
                TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();
                Task t = Task.Factory.StartNew(async () =>
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Console.WriteLine("任务2: " + DateTime.Now.ToString("ss.fff"));
                        await Task.Delay(1000);
                    }
                    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

  • 相关阅读:
    数据类型之间的转换
    博客园页面美化全系列
    drf——django的缓存和信号
    PEP8规范
    drf——drf局部使用,全局使用顺序
    drf—— xadmin的使用
    drf—— Book系列连表接口
    drf—— base64加码与解码
    drf——小知识
    drf—— 响聊聊cookie session token的演变
  • 原文地址:https://www.cnblogs.com/slwangzi/p/13213086.html
Copyright © 2020-2023  润新知