欢迎光临
我们一直在努力

c#中使用多线程二-.NET教程,C#语言

建站超值云服务器,限时71元/月

本节把form主线程从其他线程分离出来,实现数据从线程的传入传出

代码如下:

from1.cs代码如下:

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

using system.threading;

namespace student
{
 /// <summary>
 /// http://blog.csdn.net/iuhxq
 /// </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;

  private class1 c;

  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()
  {
   //
   // class1初始化
   //
   this.c = new class1();
   this.c.update += new student.class1.eventhandler(c_update);

   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”;
   //
   // 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 c_update(int index)
  {
   listview1.items[index].subitems[1].text = c.result[index].tostring();
  }

  private void button1_click(object sender, system.eventargs e)
  {
   listview1.items.add(new listviewitem(new string[]{c.result.count.tostring(),”0″}));
   c.add();
  }

  private void button2_click(object sender, system.eventargs e)
  {
   int index = c.del();
   if(index>=0)
   {
    listview1.items.removeat(index);
   }
  }
 }
}
添加类class1:

class1代码如下:

using system;
using system.collections;
using system.threading;

namespace student
{
 /// <summary>
 /// class1 的摘要说明。
 /// </summary>
 public class class1
 {

  public arraylist threads = new arraylist();
  public arraylist result = new arraylist();

  public event eventhandler update;
  public delegate void eventhandler(int index);
  
  public class1()
  {
   //
   // todo: http://blog.csdn.net/iuhxq
   //
   update += new eventhandler(class1_update);
  }

  private void process()
  {
   int i = 1;
   while(true)
   {
    i++;
    int index = threads.indexof(thread.currentthread);
    result[index] = i;
    if(update!=null)update(index);
    thread.sleep(0);
   }
  }

  public int add()
  {
   thread t = new thread(new threadstart(process));
   t.start();
   threads.add(t);
   lock(result)
   {
    result.add(0);
   }
   return threads.count-1;
  }

  public int del()
  {
   int count = threads.count;
   if(count>0)
   {
    thread t1 = (thread)threads[count-1];
    if(t1.isalive)
    {
     t1.abort();
    }
    threads.removeat(count-1);
    lock(result)
    {
     result.removeat(count-1);
    }
   }
   return count-1;
  }

  private void class1_update(int index)
  {

  }
 }
}

转载:http://blog.csdn.net/iuhxq

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#中使用多线程二-.NET教程,C#语言
分享到: 更多 (0)