首先我们创建了两个CounterCreationData对象,用于收集记录计数器的值,然后将这两个CounterCreationData对象添加到计数器数据集合CounterCreationDataCollection中。利用这个数据集合,我们就可以创建新的计数器类别了,这表示这个自定义的计数器类别会收集来自这个数据集合的计数器数据。最后,我们再创建自定义的性能计数器PerformanceCounter,并通过构造函数的参数(string categoryName, string counterName),指定这个计数器将数据保存到什么位置。
完成计数器的创建后,我们新添加了两个函数用于更新计数器的值,便于我们的应用程序调用。至此,我们自定义的文件复制计数器就实现完成了。下面我们来看看如何在我们的应用程序中使用这个自定义的性能计数器。
首先,我们需要在构造函数中完成文件复制性能计数器类的初始化工作,这一步通过调用FileCopyPerformanceCounters类的Initialize函数完成:
public MainForm()
{
this.InitializeComponent();
// 初始化计数器类
FileCopyPerformanceCounters.Initialize();
}
{
this.InitializeComponent();
// 初始化计数器类
FileCopyPerformanceCounters.Initialize();
}
完成性能计数器的初始化后,我们就可以在文件复制过程中使用计数器对文件复制过程进行监视和统计了。在“复制”按钮的响应函数中,我们用性能计数器记录文件复制数和完成的百分比:
private void BtnCopy_Click(object sender, EventArgs args)
{
// 获得源目录和目标目录
string source = this.txtSourceDirectory.Text;
string dest = this.txtDestinationDirectory.Text;
// 检查目录合法性
if (!Directory.Exists(source) || !Directory.Exists(dest))
{
MessageBox.Show("源目录或者目标目录不存在.", "Error");
return;
}
// 设置进度条
this.btnCopy.Enabled = false;
this.progressBar.Value = 0;
this.progressBar.Style = ProgressBarStyle.Continuous;
// 创建后台复制线程
this.worker = new BackgroundWorker();
// 执行复制
this.worker.DoWork += (o, e) =>
{
string[] files = Directory.GetFiles(source);
for (int i = 0; i < files.Length; ++i)
{
Thread.Sleep(1000);
File.Copy(files[i], Path.Combine(dest, Path.GetFileName(files[i])));
this.worker.ReportProgress((int)((100.0f * i) / files.Length));
// 用计数器记录已经复制的文件数
FileCopyPerformanceCounters.UpdateTotalFiles(i);
}
};
this.worker.WorkerReportsProgress = true;
this.worker.ProgressChanged += (o, e) =>
{
this.BeginInvoke((MethodInvoker)delegate
{
progressBar.Value = e.ProgressPercentage;
// 用计数器记录文件复制完成的百分比
FileCopyPerformanceCounters.UpdatePercentDone(e.ProgressPercentage);
});
};
this.worker.RunWorkerCompleted += (o, e) =>
{
this.BeginInvoke((MethodInvoker)delegate
{
btnCopy.Enabled = true;
progressBar.Style = ProgressBarStyle.Marquee;
});
};
this.worker.RunWorkerAsync();
}
{
// 获得源目录和目标目录
string source = this.txtSourceDirectory.Text;
string dest = this.txtDestinationDirectory.Text;
// 检查目录合法性
if (!Directory.Exists(source) || !Directory.Exists(dest))
{
MessageBox.Show("源目录或者目标目录不存在.", "Error");
return;
}
// 设置进度条
this.btnCopy.Enabled = false;
this.progressBar.Value = 0;
this.progressBar.Style = ProgressBarStyle.Continuous;
// 创建后台复制线程
this.worker = new BackgroundWorker();
// 执行复制
this.worker.DoWork += (o, e) =>
{
string[] files = Directory.GetFiles(source);
for (int i = 0; i < files.Length; ++i)
{
Thread.Sleep(1000);
File.Copy(files[i], Path.Combine(dest, Path.GetFileName(files[i])));
this.worker.ReportProgress((int)((100.0f * i) / files.Length));
// 用计数器记录已经复制的文件数
FileCopyPerformanceCounters.UpdateTotalFiles(i);
}
};
this.worker.WorkerReportsProgress = true;
this.worker.ProgressChanged += (o, e) =>
{
this.BeginInvoke((MethodInvoker)delegate
{
progressBar.Value = e.ProgressPercentage;
// 用计数器记录文件复制完成的百分比
FileCopyPerformanceCounters.UpdatePercentDone(e.ProgressPercentage);
});
};
this.worker.RunWorkerCompleted += (o, e) =>
{
this.BeginInvoke((MethodInvoker)delegate
{
btnCopy.Enabled = true;
progressBar.Style = ProgressBarStyle.Marquee;
});
};
this.worker.RunWorkerAsync();
}
在这段代码中,我们通过在文件复制Lambda表达式中调用FileCopyPerformanceCounters类的UpdateTotalFiles函数,实时地对性能计数器进行了更新,对复制的文件数进行了监视。同样地,在进度条更新的Lambda表达式中,我们对当前复制进度的百分比计数器进行了更新。