c#中使用多线程(图)[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

= "线程编号";
   this.columnHeader1.Width = 81;
   //
   // columnHeader2
   //
   this.columnHeader2.Text = "value";
   this.columnHeader2.Width = 180;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Controls.Add(this.listView1);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
  
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   Add();
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   Del();
  }

  private void button3_Click(object sender, System.EventArgs e)
  {

   while(threads.Count>0)
   {
    Thread t1 = (Thread)threads[0];
    if(t1.IsAlive)
    {
     t1.Abort();
    }
    threads.RemoveAt(0);
    lock(listView1)
    {
     listView1.Items.RemoveAt(0);
    }
   }
  }

  private void Add()
  {
   int count = threads.Count;
   if(count<10)
   {
    Thread t = new Thread(new ThreadStart(Process));
    t.Start();
    threads.Add(t);
    lock(listView1)
    {
     listView1.Items.Insert(count, new ListViewItem(new string[]{count.ToString(),"0"}));
    }
   }
  }

  private void Del()
  {
   int count = threads.Count;
   if(count>0)
   {
    Thread t1 = (Thread)threads[count-1];
    if(t1.IsAlive)
    {
     t1.Abort();
    }
    threads.RemoveAt(count-1);
    lock(listView1)
    {
     listView1.Items.RemoveAt(count-1);
    }
   }
  }

本文关键:c#中使用多线程(图)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top