• 处理耗时任务


    目录

    一 设计原型

    二 后台源码


    一 设计原型

    二 后台源码

    1. namespace 处理耗时任务
    2. {
    3. public partial class Form1 : Form
    4. {
    5. public Form1()
    6. {
    7. InitializeComponent();
    8. }
    9. bool IsRun = false;
    10. private string path = Directory.GetCurrentDirectory() + "\\古诗词.txt";
    11. private void button1_Click(object sender, EventArgs e)
    12. {
    13. IsRun = true;
    14. }
    15. private void button2_Click(object sender, EventArgs e)
    16. {
    17. IsRun = false;
    18. }
    19. private void Form1_Load(object sender, EventArgs e)
    20. {
    21. label1.BackColor = Color.Green;
    22. string[] cons = File.ReadAllLines(path);
    23. int index = 0;
    24. Task.Run(() =>
    25. {
    26. while (true)
    27. {
    28. if (IsRun)
    29. {
    30. this.BeginInvoke(() =>
    31. {
    32. progressBar1.Value += 1;
    33. label1.Text = progressBar1.Value.ToString() + "%";
    34. if (progressBar1.Value == 100)
    35. {
    36. progressBar1.Value = 0;
    37. }
    38. if (index == cons.Length)
    39. {
    40. index = 0;
    41. richTextBox1.Clear();
    42. }
    43. richTextBox1.AppendText(cons[index] + "\r\n");
    44. index++;
    45. });
    46. }
    47. Thread.Sleep(100);
    48. }
    49. });
    50. }
    51. }
    52. }

    设计器自动生成代码

    1. namespace 处理耗时任务
    2. {
    3. partial class Form1
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. progressBar1 = new ProgressBar();
    29. button1 = new Button();
    30. button2 = new Button();
    31. label1 = new Label();
    32. richTextBox1 = new RichTextBox();
    33. SuspendLayout();
    34. //
    35. // progressBar1
    36. //
    37. progressBar1.Location = new Point(22, 14);
    38. progressBar1.Name = "progressBar1";
    39. progressBar1.Size = new Size(817, 23);
    40. progressBar1.Step = 1;
    41. progressBar1.TabIndex = 0;
    42. //
    43. // button1
    44. //
    45. button1.Location = new Point(301, 638);
    46. button1.Name = "button1";
    47. button1.Size = new Size(94, 29);
    48. button1.TabIndex = 1;
    49. button1.Text = "开始任务";
    50. button1.UseVisualStyleBackColor = true;
    51. button1.Click += button1_Click;
    52. //
    53. // button2
    54. //
    55. button2.Location = new Point(429, 638);
    56. button2.Name = "button2";
    57. button2.Size = new Size(94, 29);
    58. button2.TabIndex = 2;
    59. button2.Text = "暂停任务";
    60. button2.UseVisualStyleBackColor = true;
    61. button2.Click += button2_Click;
    62. //
    63. // label1
    64. //
    65. label1.AutoSize = true;
    66. label1.Location = new Point(382, 14);
    67. label1.Name = "label1";
    68. label1.Size = new Size(0, 20);
    69. label1.TabIndex = 3;
    70. //
    71. // richTextBox1
    72. //
    73. richTextBox1.Location = new Point(12, 46);
    74. richTextBox1.Name = "richTextBox1";
    75. richTextBox1.Size = new Size(827, 586);
    76. richTextBox1.TabIndex = 4;
    77. richTextBox1.Text = "";
    78. //
    79. // Form1
    80. //
    81. AutoScaleDimensions = new SizeF(9F, 20F);
    82. AutoScaleMode = AutoScaleMode.Font;
    83. ClientSize = new Size(841, 670);
    84. Controls.Add(richTextBox1);
    85. Controls.Add(label1);
    86. Controls.Add(button2);
    87. Controls.Add(button1);
    88. Controls.Add(progressBar1);
    89. MaximizeBox = false;
    90. MinimizeBox = false;
    91. Name = "Form1";
    92. Text = "耗时任务";
    93. Load += Form1_Load;
    94. ResumeLayout(false);
    95. PerformLayout();
    96. }
    97. #endregion
    98. private ProgressBar progressBar1;
    99. private Button button1;
    100. private Button button2;
    101. private Label label1;
    102. private RichTextBox richTextBox1;
    103. }
    104. }

  • 相关阅读:
    Flask简单调用Redis、MySQL和生成token及token验证
    大数据安全 | 【实验】RSA加密解密
    广告牌安全监测,保障户外广告牌的安全与稳定
    Java(九)----File类
    《知识点累积》Vue2.x
    ue4 unreal cpp c++ 检测 检测 端口是否占用
    UNIAPP实战项目笔记34 购物车的单选和全选单选联动
    现代图片性能优化及体验优化指南 - 缩放精细化展示及避免布局偏移、拉伸
    来吧!再谈多线程
    Rasa-笔记
  • 原文地址:https://blog.csdn.net/XiaoWang_csdn/article/details/139785040