首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 软件教学-> 压缩工具
c#中使用多线程二-.NET教程,C#语言
作者:网友供稿 点击:54
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

本节把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


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·web打印的另类方法-ASP教程,打印相关
·Java简单类型进行精确浮点数运算-JSP教程,Java技巧及代码
·使用JAVAMAIL发邮件的一个例子-JSP教程,邮件相关
·中国移动本地传输网建设方案初探
·传输系统中的时钟同步技术
·JSP学习经验总结(转)-JSP教程,Jsp/Servlet
·WEB打印,去页眉和页脚-ASP教程,打印相关
·Native XML数据库技术详解-.NET教程,XML应用
·jsp生成html--readtemplates-JSP教程,Jsp/Servlet
·下拉框反回选定的文字 (修改页面经常用到)-ASP教程,ASP应用
最新文章
·在xp中如何使用windows vista屏保_windows xp
·photoshop调色:cmyk模式处理单色调特效_photoshop教程
·视频分享网站视频广告发展面临的问题_营销推广
·难以置信,这样的网站每月盈利上千万_营销推广
·大站做百度主题推广 小站做google adsense_网赚技巧
·方兴东:保留alexa插件才能给站长带来快乐_站长访谈
·网友天下ceo叶灵:欢迎大家看web2.0笑话_站长访谈
·我们应该如何运营网站_站长心得
·网站从业者还要更加务实才能赚钱_站长心得
·炼成高级网络编辑的三大原则_站长心得
相关主题
  • c#中使用存储过程中的返回值_c#应用
  • c#中使用textbox控件的两个问题_c#应用
  • c#中使用net share命令时要注意的问题_c#应用
  • c#中使用dts来导入数据及相关问题 _c#应用
  • c#中使用多线程-.NET教程,C#语言
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭