All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
如何最大限度提高.NET的性能,(续)_.net资料_编程技术
控制电脑关机或者重新启动的JS代码_JavaScript技术_编程技术
网页申请表单用户体验设计简述_[Html教程]
C#中使用net,share命令时要注意的问题_.net资料_编程技术
C#教程:线程的优先权
delphi获取指定磁盘中剩余空间
获取所有用户表及根据表Id取得表字段信息_[SQL,Server教程]
SQL,Server,全局变量_mssql学习_编程技术
SQL2000 关于 Java JDBC 驱动的安装和设定_[SQL Server教程]
SQL,Server,7.0,入门(七)_mssql学习_编程技术
Reading number is top 10 pictures
Summer is most suitable for young people to travel in China4
2012 national geographic daily picture7
The real super beauty4
中国处女图鉴1
Sora aoi on twitter2
运动的范冰冰2
The real super beauty2
到南昌西站了3
西班牙山村小景4
你的钱干净吗?
Download software ranking
DreamWeaver8
WebService在.NET中的实战应用教学视频 → 第2集
Boxer's Top ten classic battle6
Boxer vs Yellow4
Rio big adventure
仙剑奇侠传98硬盘WINXP版
asp.net技术内幕
Tram sex maniac 2 (H) rar bag3
Sora aoi - one of more PK
WebService在.NET中的实战应用教学视频 → 第4集
qq published in(发表于) 2014/7/11 9:28:09 Edit(编辑)
C#教程:线程应用实例

C#教程:线程应用实例

C#教程:线程应用实例

线程应用实例

如果在一个软件开发项目中,需要将数据脱机,如保存在文件过程中,然后再将文件中的数据进行处理,这时就需要实时的监控一个目录中的文件数量以判断文件中的数据是否被处理。下面将通过一个例子说明线程在程序中的应用。在这个例子中要建立一个永久线程,这个永久线程将每隔一段时间就遍历指定的一个文件夹,并计算出被监视的文件夹及其子文件夹中文件的数量。

程序开发步骤如下所示。

(1)在VS2005中新建一个Windows应用程序项目,并命名为ThreadMonitorFile。

(2)打开Windows窗体设计器将窗体重命名为FrmMain,并将Text属性设为“FrmMain”。在FrmMain窗体添加一个TextBox控件、一个Button控件和一个Lable控件。将Lable的Text属性设为“监视的路径:”,Button的Text属性设为“开始监视”。具体界面设计如图1所示。



图1 线程实例主窗体界面设计图

(3)添加一个Windows窗体,并命名为frmThread。在frmThread窗体上添加3个Lable控件。将Lable1控件的Text属性设为“监视的路径:”,Lable2的Text属性设为“正在查询监视路径下文件数量,请等待。”,Lable3的Text属性设为“文件的数量:”。具体界面设计如图2所示。



图2 线程实例frmThread窗体界面设计图

(4)双击FrmMain窗体的【开始监视】按钮,给窗体添加一个静态成员,代码如下:

public static string path;

在按钮的双击事件中调用frmThread窗体。FrmMain窗体调用frmThread窗体的代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace ThreadMonitorFile

{

public partial class FrmMain : Form

{

public static string path;

public FrmMain()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

FrmMain.path = this.textBox1.Text;

frmThread frm = new frmThread();

frm.Show();

}

}

}

(5)双击frmThread窗体,为窗体添加两个成员变量和一个委托。代码如下:

private int j = 0;

private Thread t = null;

delegate void SetTextCallback(string text);

同时为窗体添加GetAllFiles()方法遍历文件夹中文件方法,Forver()方法永久线程调用的方法,SetText()方法Windows窗体控件线程安全调用方法。具体代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Threading;

namespace ThreadMonitorFile

{

public partial class frmThread : Form

{

private int j = 0;

private Thread t = null;

delegate void SetTextCallback(string text);

public frmThread()

{

InitializeComponent();

}

private void frmThread_Load(object sender, EventArgs e)

{

label3.Text = FrmMain.path;

t = new Thread(new ThreadStart(Forver));

t.Start();

}

public int GetAllFiles(DirectoryInfo dir)

{

FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.