我做 了一个文档扫描的软件,扫描的时候使用了多线程技术,并且对每个扫描都会弹出一个进度条的显示,现在的问题是我连续扫描三个不同的文件的时候,所有的进度条操作都在最后开的那个进度条里进行,小弟是第一次玩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)
{ }
}
}谢谢大家乐
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)
{ }
}
}谢谢大家乐
解决方案 »
- C#读取注册表的值时提示“未将对象实例化”
- 怎么给textbox添加图片?
- 2010年8月16号左右 有人到上海塔塔入职吗
- 怎么关闭控制台应用程序
- 求助 C# 在dataGridView中加ComboBox 并帮定到相应数据库的字段上 源码 ~~~~~~~~~~~~~~~~急!
- 怎么我搜索出来的数据这么乱!!!!(帮帮忙啊!!!)
- bit字段对应c#中的数据类型
- <asp:datalist 的情况下好像用 e.Item.Cells[5].Text="";不行?
- 如何在windows forms 的菜单上画图标???
- ListView 里的Item有没有双击事件,如果没有,有什么可以变通的方法?
- AJAX htmlfile: 未知的运行时错误(求指导)
- C#winform 程序,求高手帮我挑错!为什么我的int a=da.Update(ds, "login");中a的值不变(为默认值0)
private void ShowProcessBar()
{
myProcessBar = new Form2();
myIncrease = new IncreaseHandle(myProcessBar.Increase);
myProcessBar.Show();
myProcessBar = null;
}问题出在这
第一个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); }
大概是这样,你试一下