线程池的使用实现窗口控件安全操作

namespace TestThreadPath {     partial class form_thread     {         /// <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.label1 = new System.Windows.Forms.Label();             this.tb_pathname = new System.Windows.Forms.TextBox();             this.label_threadcount = new System.Windows.Forms.Label();             this.tb_threadcount = new System.Windows.Forms.TextBox();             this.but_begin = new System.Windows.Forms.Button();             this.SuspendLayout();             //             // label1             //             this.label1.AutoSize = true;             this.label1.Location = new System.Drawing.Point(21, 22);             this.label1.Name = "label1";             this.label1.Size = new System.Drawing.Size(53, 12);             this.label1.TabIndex = 0;             this.label1.Text = "路径名:";             //             // tb_pathname             //             this.tb_pathname.Location = new System.Drawing.Point(80, 13);             this.tb_pathname.Name = "tb_pathname";             this.tb_pathname.Size = new System.Drawing.Size(188, 21);             this.tb_pathname.TabIndex = 1;             //             // label_threadcount             //             this.label_threadcount.AutoSize = true;             this.label_threadcount.Location = new System.Drawing.Point(21, 55);             this.label_threadcount.Name = "label_threadcount";             this.label_threadcount.Size = new System.Drawing.Size(53, 12);             this.label_threadcount.TabIndex = 2;             this.label_threadcount.Text = "线程数:";             //             // tb_threadcount             //             this.tb_threadcount.Location = new System.Drawing.Point(80, 46);             this.tb_threadcount.Name = "tb_threadcount";             this.tb_threadcount.Size = new System.Drawing.Size(100, 21);             this.tb_threadcount.TabIndex = 3;             //             // but_begin             //             this.but_begin.Location = new System.Drawing.Point(187, 46);             this.but_begin.Name = "but_begin";             this.but_begin.Size = new System.Drawing.Size(75, 23);             this.but_begin.TabIndex = 4;             this.but_begin.Text = "开始";             this.but_begin.UseVisualStyleBackColor = true;             this.but_begin.Click += new System.EventHandler(this.but_begin_Click);             //             // form_thread             //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.BackColor = System.Drawing.Color.White;             this.ClientSize = new System.Drawing.Size(298, 307);             this.Controls.Add(this.but_begin);             this.Controls.Add(this.tb_threadcount);             this.Controls.Add(this.label_threadcount);             this.Controls.Add(this.tb_pathname);             this.Controls.Add(this.label1);             this.MaximizeBox = false;             this.Name = "form_thread";             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;             this.Text = "线程测试";             this.ResumeLayout(false);             this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;         private System.Windows.Forms.TextBox tb_pathname;         private System.Windows.Forms.Label label_threadcount;         private System.Windows.Forms.TextBox tb_threadcount;         private System.Windows.Forms.Button but_begin;     } }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; namespace TestThreadPath {     public partial class form_thread : Form     {         static int threadcount;         Label[] threadLabel;         Label[] pathLabel;         DirectoryInfo dir;         static FileInfo[] finfo;

        Boolean initthreadcount=false;         Boolean initpath=false;

        public form_thread()         {             InitializeComponent();         }

        private void but_begin_Click(object sender, EventArgs e)         {             if (tb_threadcount.Text.Length > 0)                 threadcount = Convert.ToInt16(tb_threadcount.Text.Trim());             else             {                 threadcount = 0;                 MessageBox.Show("请输入线程数!");                 return;             }

            if (threadcount > 5)                 MessageBox.Show("不要这么猛吧,线程太多啦!");             else                 initthreadcount = true;            

            if (tb_pathname.Text.Length > 0)             {                 dir = new DirectoryInfo(@tb_pathname.Text);                 if (dir.Exists)                 {                     finfo = dir.GetFiles();                     initpath = true;                 }                 else                 {                     MessageBox.Show("路径不存在!");                     return;                 }             }             else             {                 MessageBox.Show("请输入路径!");                 return;             }                                  if (initthreadcount&&initpath)             {                 threadLabel = new Label[threadcount];                 pathLabel=new Label[threadcount];                 for (int i = 0; i < threadcount; i++)                 {                     threadLabel[i] = new Label();                     threadLabel[i].Text="路径"+i.ToString()+":";                     threadLabel[i].Location = new Point(20, 55 + 30 * (i + 1));

                    pathLabel[i] = new Label();                     pathLabel[i].Text = "path" + i.ToString() + ":";                     pathLabel[i].Location = new Point(80, 55 + 30 * (i + 1));                                         this.Controls.Add(pathLabel[i]);                     this.Controls.Add(threadLabel[i]);                 }             }

            WaitCallback setLabelCallback = new WaitCallback(SetPathLabel);

            for(int j=0;j<threadcount;j++)             {                 ThreadPool.QueueUserWorkItem(setLabelCallback, pathLabel[j]);             }

                    }

        public static void SetPathLabel(object labelid)         {             Random rndm = new Random();             while (true)             {                 int pathindex = rndm.Next(threadcount);                 SetLabelText((Label)labelid, finfo[pathindex].Name.ToString());                 Thread.Sleep(2000);                            }         }

 

        private static void DoThreadSafe(Control control, MethodInvoker function)         {             if (function != null)             {                 if (control.InvokeRequired)                 {                     control.Invoke(function);                 }                 else                 {                     function();                 }             }         }

        private static void SetLabelText(Label setLable,String setText)         {             DoThreadSafe(setLable, delegate             {                 setLable.Text = setText;             });         }  

 

 

 

    } }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值