处理耗时任务

目录

一 设计原型

二 后台源码


一 设计原型

二 后台源码

namespace 处理耗时任务
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool IsRun = false;
        private string path = Directory.GetCurrentDirectory() + "\\古诗词.txt";

        private void button1_Click(object sender, EventArgs e)
        {
            IsRun = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IsRun = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.BackColor = Color.Green;
            string[] cons = File.ReadAllLines(path);
            int index = 0;

            Task.Run(() =>
            {
                while (true)
                {
                    if (IsRun)
                    {
                        this.BeginInvoke(() =>
                        {
                            progressBar1.Value += 1;
                            label1.Text = progressBar1.Value.ToString() + "%";
                            if (progressBar1.Value == 100)
                            {
                                progressBar1.Value = 0;

                            }

                            if (index == cons.Length)
                            {
                                index = 0;
                                richTextBox1.Clear();
                            }

                            richTextBox1.AppendText(cons[index] + "\r\n");
                            index++;

                        });
                    }
                    Thread.Sleep(100);
                }
            });
        }

    }
}

设计器自动生成代码:

namespace 处理耗时任务
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            progressBar1 = new ProgressBar();
            button1 = new Button();
            button2 = new Button();
            label1 = new Label();
            richTextBox1 = new RichTextBox();
            SuspendLayout();
            // 
            // progressBar1
            // 
            progressBar1.Location = new Point(22, 14);
            progressBar1.Name = "progressBar1";
            progressBar1.Size = new Size(817, 23);
            progressBar1.Step = 1;
            progressBar1.TabIndex = 0;
            // 
            // button1
            // 
            button1.Location = new Point(301, 638);
            button1.Name = "button1";
            button1.Size = new Size(94, 29);
            button1.TabIndex = 1;
            button1.Text = "开始任务";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            // 
            // button2
            // 
            button2.Location = new Point(429, 638);
            button2.Name = "button2";
            button2.Size = new Size(94, 29);
            button2.TabIndex = 2;
            button2.Text = "暂停任务";
            button2.UseVisualStyleBackColor = true;
            button2.Click += button2_Click;
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new Point(382, 14);
            label1.Name = "label1";
            label1.Size = new Size(0, 20);
            label1.TabIndex = 3;
            // 
            // richTextBox1
            // 
            richTextBox1.Location = new Point(12, 46);
            richTextBox1.Name = "richTextBox1";
            richTextBox1.Size = new Size(827, 586);
            richTextBox1.TabIndex = 4;
            richTextBox1.Text = "";
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(9F, 20F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(841, 670);
            Controls.Add(richTextBox1);
            Controls.Add(label1);
            Controls.Add(button2);
            Controls.Add(button1);
            Controls.Add(progressBar1);
            MaximizeBox = false;
            MinimizeBox = false;
            Name = "Form1";
            Text = "耗时任务";
            Load += Form1_Load;
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private ProgressBar progressBar1;
        private Button button1;
        private Button button2;
        private Label label1;
        private RichTextBox richTextBox1;
    }
}

相关推荐

  1. php多进程处理任务

    2024-06-19 10:04:05       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-19 10:04:05       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-19 10:04:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-19 10:04:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-19 10:04:05       18 阅读

热门阅读

  1. kotlin 注解 @Parcelize 使用

    2024-06-19 10:04:05       12 阅读
  2. Hadoop+Spark大数据技术(自命题试卷测试)

    2024-06-19 10:04:05       10 阅读
  3. Bash sleep随机时间

    2024-06-19 10:04:05       9 阅读
  4. elasticsearch安装(centos7)

    2024-06-19 10:04:05       6 阅读
  5. Vue3使用component动态展示组件

    2024-06-19 10:04:05       7 阅读
  6. 前端面试题——网络篇

    2024-06-19 10:04:05       6 阅读
  7. 数据传输安全(为支付宝第三方做铺垫)

    2024-06-19 10:04:05       9 阅读
  8. HTML(6)——表单

    2024-06-19 10:04:05       8 阅读
  9. 【数据结构】练习集

    2024-06-19 10:04:05       6 阅读
  10. template标签

    2024-06-19 10:04:05       7 阅读
  11. Springboot应用设置跳过SSL证书认证

    2024-06-19 10:04:05       8 阅读