关于C#多线程进度条的问题
我做 了一个文档扫描的软件,扫描的时候使用了多线程技术,并且对每个扫描都会弹出一个进度条的显示,现在的问题是我连续扫描三个不同的文件的时候,所有的进度条操作都在最后开的那个进度条里进行,小弟是第一次玩C#,不是很懂怎么调,接下来就是贴源代码,大家帮我看看。首先是进度条窗体using System;然后是主界面
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string path1;
public Form2()
{
InitializeComponent();
}
public bool Increase(int nValue)
{
if (nValue > 0)
{
if (progressBar2.Value + nValue < progressBar2.Maximum)
{
progressBar2.Value += nValue;
return true;
}
else
{
progressBar2.Value = progressBar2.Maximum;
progressBar2.Value = 0;
}
}
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string path1;
public Form1()
{
InitializeComponent();
}
private Form2 myProcessBar = null;
private delegate bool IncreaseHandle(int nValue);
private IncreaseHandle myIncrease = null;
private void ShowProcessBar()
{
myProcessBar = new Form2();
myIncrease = new IncreaseHandle(myProcessBar.Increase);
myProcessBar.Show();
myProcessBar = null;
}
private void ScanFile()
{
StreamReader aFile = new StreamReader(path1, System.Text.Encoding.Default);
String name = Path.GetFileName(path1);
String role1 = @"绝.*密";
Regex reg1 = new Regex(role1);
MethodInvoker mi = new MethodInvoker(ShowProcessBar);
this.BeginInvoke(mi);
Thread.Sleep(1000);
object objReturn = null;
while (aFile.Peek() >= 0)
{
Thread.Sleep(1);
objReturn = this.Invoke(this.myIncrease, new object[] { 2 });
string line = aFile.ReadLine();
if (reg1.IsMatch(line))
{
MessageBox.Show("该文档涉密", name);
return;
}
}
MessageBox.Show("该文档不涉密", name);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.FileName = "";
ofd1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd1.Filter = "文本文件(*.txt)|*.txt";
ofd1.ValidateNames = true;
ofd1.CheckPathExists = true;
ofd1.CheckFileExists = true;
if(ofd1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd1.FileName;
path1 = textBox1.Text;
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
ThreadStart scanStart = new ThreadStart(ScanFile);
Thread scaning = new Thread(scanStart);
scaning.Name = path1;
scaning.IsBackground = true;
scaning.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
谢谢大家乐 --------------------编程问答-------------------- 是不是代码哪地方写错了,仔细检查检查! --------------------编程问答-------------------- 自己顶一个,真的是有问题不会,不是自身代码敲错的问题,大神们帮我看看吧 --------------------编程问答-------------------- 大哥,发的代码加点注释呀,读的太费劲了
private void ShowProcessBar()
{
myProcessBar = new Form2();
myIncrease = new IncreaseHandle(myProcessBar.Increase);
myProcessBar.Show();
myProcessBar = null;
}
问题出在这 --------------------编程问答-------------------- Form2 A = new Form2();
第一个Form2是个类型定义
A是指针(存类型分配的内存的首地址)
new Form2()是让系统分配一块内存
A = new Form2()就是把这块内存的地址存在A的变量里,你就可以用了
——————————————————————
A = new Form2();
A = new Form2();
A = new Form2();
感觉是一样的吧,但三个new Form2()不是一个东西,是三块内存。上面三行执行完后,A里存的是最后的那块内存的地址。所以你的更新全在最后的那个窗口里
--------------------编程问答-------------------- 改一下
private Form2 myProcessBar = null;--这个不行
private Dictionary<string, Form2> myProcessBar = new Dictionary<string, Form2>();
private Dictionary<string, IncreaseHandle> myIncrease = new Dictionary<string, IncreaseHandle>();
private void ShowProcessBar(string name)
{
myProcessBar[name] = new Form2();
myIncrease[name] = new IncreaseHandle(myProcessBar[name].Increase);
myProcessBar[name].Show();
}
private void ScanFile()
{
StreamReader aFile = new StreamReader(path1, System.Text.Encoding.Default);
String name = Path.GetFileName(path1);
MethodInvoker mi = new MethodInvoker(ShowProcessBar); //这块加参数,把name传进去
//…………………………………………
while (aFile.Peek() >= 0)
{
Thread.Sleep(1);
objReturn = this.Invoke(this.myIncrease[name], new object[] { 2 }); //这块有变动
string line = aFile.ReadLine();
if (reg1.IsMatch(line))
{
MessageBox.Show("该文档涉密", name);
return;
}
}
MessageBox.Show("该文档不涉密", name);
}
大概是这样,你试一下
补充:.NET技术 , C#