首页 > 开发 > 综合 > 正文

c#中使用多线程(图)一

2024-07-21 02:26:53
字体:
来源:转载
供稿:网友

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;

using system.threading;

namespace student
{
 /// <summary>
 /// form1 的摘要说明。
 /// </summary>
 public class form1 : system.windows.forms.form
 {
  private system.windows.forms.button button1;
  private system.windows.forms.button button2;
  private system.windows.forms.button button3;

  arraylist threads = new arraylist();

  private system.windows.forms.listview listview1;
  private system.windows.forms.columnheader columnheader1;
  private system.windows.forms.columnheader columnheader2;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private system.componentmodel.container components = null;

  public form1()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {
   this.button1 = new system.windows.forms.button();
   this.button2 = new system.windows.forms.button();
   this.button3 = new system.windows.forms.button();
   this.listview1 = new system.windows.forms.listview();
   this.columnheader1 = new system.windows.forms.columnheader();
   this.columnheader2 = new system.windows.forms.columnheader();
   this.suspendlayout();
   //
   // button1
   //
   this.button1.location = new system.drawing.point(24, 216);
   this.button1.name = "button1";
   this.button1.tabindex = 1;
   this.button1.text = "add";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // button2
   //
   this.button2.location = new system.drawing.point(104, 216);
   this.button2.name = "button2";
   this.button2.tabindex = 2;
   this.button2.text = "del";
   this.button2.click += new system.eventhandler(this.button2_click);
   //
   // button3
   //
   this.button3.location = new system.drawing.point(184, 216);
   this.button3.name = "button3";
   this.button3.tabindex = 3;
   this.button3.text = "delall";
   this.button3.click += new system.eventhandler(this.button3_click);
   //
   // listview1
   //
   this.listview1.columns.addrange(new system.windows.forms.columnheader[] {
                      this.columnheader1,
                      this.columnheader2});
   this.listview1.fullrowselect = true;
   this.listview1.gridlines = true;
   this.listview1.location = new system.drawing.point(0, 0);
   this.listview1.name = "listview1";
   this.listview1.size = new system.drawing.size(288, 208);
   this.listview1.tabindex = 5;
   this.listview1.view = system.windows.forms.view.details;
   //
   // columnheader1
   //
   this.columnheader1.text = "线程编号";
   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);
    }
   }
  }


  private void process()
  {
   int i = 1;
   while(true)
   {
    i++;
    int j = threads.indexof(thread.currentthread);
    lock(listview1)
    {
     listview1.items[j].subitems[1].text = i.tostring();
    }
    thread.sleep(0);
   }
  }
 }
}

商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表