还是比较容易上手的。 使用ProgressBar控件。下面的代码是copy多个文件时显示进度条的代码。 若你读取一个文件要显示进度条,必须把文件分成多块来读,每读完一块,进度条前进相应的步数,楼主自己去了解一下。代码是单线程的。 private void CopyWithProgress(string[] filenames) { // Display the ProgressBar control. pBar1.Visible = true; // Set Minimum to 1 to represent the first file being copied. pBar1.Minimum = 1; // Set Maximum to the total number of files to copy. pBar1.Maximum = filenames.Length; // Set the initial value of the ProgressBar. pBar1.Value = 1; // Set the Step property to a value of 1 to represent each file being copied. pBar1.Step = 1;
// Loop through all files to copy. for (int x = 1; x <= filenames.Length; x++) { // Copy the file and increment the ProgressBar if successful. if(CopyFile(filenames[x-1]) == true) { // Perform the increment on the ProgressBar. pBar1.PerformStep(); } } }
使用接口的方法1.定义一个简单的接口 public interface IProgressBar { ProgressBar ProgressBar {get;} }2.你的类处理文件里使用这个接口 public class MyFile { public MyFile(IProgressBar progressBar){...} }这样任何实现了IProgressBar的类都能传给你的文件处理类如果你要用主窗口的进度条,主窗口实现接口IProgressBar。
使用ProgressBar控件。下面的代码是copy多个文件时显示进度条的代码。
若你读取一个文件要显示进度条,必须把文件分成多块来读,每读完一块,进度条前进相应的步数,楼主自己去了解一下。代码是单线程的。
private void CopyWithProgress(string[] filenames)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
pBar1.Step = 1;
// Loop through all files to copy.
for (int x = 1; x <= filenames.Length; x++)
{
// Copy the file and increment the ProgressBar if successful.
if(CopyFile(filenames[x-1]) == true)
{
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
}
}
}
=========================
把主窗口中驱动进度条的类公布为public的,比如这个CopyWithProgress方法。
然后(form1)(form2.Parent).CopyWithProgress()
再问一下:
在unsafe{.....}能使用(form1)(form2.Parent).CopyWithProgress()
吗?
public interface IProgressBar
{
ProgressBar ProgressBar {get;}
}2.你的类处理文件里使用这个接口
public class MyFile
{
public MyFile(IProgressBar progressBar){...}
}这样任何实现了IProgressBar的类都能传给你的文件处理类如果你要用主窗口的进度条,主窗口实现接口IProgressBar。
我是MainForm------>调用MdiForm----->使用自己的读文件的类。
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
MdiForm myMdiChild = new MdiForm();
MdiChild.Show();
}2.在MdiForm中的Load中打开文件
private void Form2_Load(object sender, EventArgs e)
{
ReadF.RDfile("c:\\text.dat");
}
3.以下是读文件的类namespace myPRJ
{
public class ReadF
{
public static RDfile(string sFileName)
{
FileStream fstream = new FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.None);
byte[] bBuffer = new byte[5800];
for(int i=0;i<10000;i++)
{
fstream.Read(bBuffer, 0, 5800);
....处理数据
》》》》》》》》我想在这个地方使用MainForm中的toolStripProgressBar1
}
fstream.Close();
}
}
}
如果你这样做会导致很多不明错误。
如果你是在.net 2.0基础上开发建议使用BackGroundWorker类。
否则你需要使用Control中可以进行异步编程的方法
用过用多线程的话,用invoke调用