★搜Asp.net★(www.soAsp.net),为专业技术文档网站。
包括Asp.net开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档·VB.NET开发技术文档。
还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。
线程应用实例--监视的文件夹及其子文件夹中文件的数量

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

程序开发步骤:

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

2)打开Windows窗体设计器将窗体重命名为FrmMain,并将Text属性设为“FrmMain”。在FrmMain窗体添加一个TextBox控件、一个Button控件和一个Lable控件。将LableText属性设为“监视的路径:”,ButtonText属性设为“开始监视”。

3)添加一个Windows窗体,并命名为frmThread。在frmThread窗体上添加4Lable控件。并将Lable1控件的Text属性设为“监视的路径:”,Lable2Text属性设为“正在查询监视路径下文件数量,请等待。”Lable2Text属性设为“ 文件的数量”。

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();
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)
                {
                    GetAllFiles((DirectoryInfo)i);
                }
                else
                {
                    j++;
                }
            }
            return j;
        }
        public void Forver()
        {
            while (true)
            {
                DirectoryInfo dir = new DirectoryInfo(FrmMain.path);
                j = 0;
                SetText(GetAllFiles(dir).ToString());
                Thread.Sleep(3000);
            }
        }
        /// <summary>
        /// 线程安全访问控件。
        /// </summary>
        /// <param name="text">文件数量</param>
        private void SetText(string text)
        {
            if (this.label2.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.label2.Text = text;
            }
        }
 
        private void frmThread_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (t.IsAlive)
            {
                t.Abort();
            }
        }
 
    }
}

6)编译后,程序运行

7)单击“开始监视”按钮,将自动获得所监视路径下的文件数量

上面的这个实例是一个多线程应用的典型实例,可以向监视目录中复制大量文件,或者写一个程序向监视目录中复制大量文件,这样实时监视的效果更明显。在使用多线程的过程中要注意在关闭程序时将线程关闭。

无标题页

无标题页
网站主页 | 广告业务 | 责任声明 | 联系我们 | 关于本站
2008 soAsp.net 使用前必读 京ICP备08009904号
本站为公益性网站,提供开发技术参考文章,不代表本站观点,不保证文章正确性。