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; }); }
} }