国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

c#中使用多線程(圖)二

2024-07-21 02:26:54
字體:
來源:轉載
供稿:網友

接上節多線程學習:http://blog.csdn.net/iuhxq/archive/2005/10/12/500295.aspx

本節把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)
  {

  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普陀区| 中超| 南康市| 务川| 罗田县| 晋城| 大名县| 铜梁县| 万州区| 保山市| 永寿县| 浦县| 崇州市| 安吉县| 和林格尔县| 平遥县| 临夏市| 丹江口市| 德保县| 海淀区| 东山县| 兰考县| 清远市| 洞口县| 巫溪县| 大冶市| 韶关市| 宁化县| 桐城市| 景东| 深州市| 钟山县| 庆安县| 嘉兴市| 临汾市| 定陶县| 梓潼县| 梓潼县| 广德县| 孙吴县| 云林县|