问题描述
我做了一个文档扫描的软件,扫描的时候使用了多线程技术,并且对每个扫描都会弹出一个进度条的显示,现在的问题是我连续扫描三个不同的文件的时候,所有的进度条操作都在最后开的那个进度条里进行,小弟是第一次玩C#,不是很懂怎么调,接下来就是贴源代码,大家帮我看看。首先是进度条窗体usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;usingSystem.IO;usingSystem.Text.RegularExpressions;namespaceWindowsFormsApplication1{publicpartialclassForm2:Form{publicstringpath1;publicForm2(){InitializeComponent();}publicboolIncrease(intnValue){if(nValue>0){if(progressBar2.Value+nValue<progressBar2.Maximum){progressBar2.Value+=nValue;returntrue;}else{progressBar2.Value=progressBar2.Maximum;progressBar2.Value=0;}}returnfalse;}}}
然后是主界面usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Text.RegularExpressions;usingSystem.Threading;namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{publicstringpath1;publicForm1(){InitializeComponent();}privateForm2myProcessBar=null;privatedelegateboolIncreaseHandle(intnValue);privateIncreaseHandlemyIncrease=null;privatevoidShowProcessBar(){myProcessBar=newForm2();myIncrease=newIncreaseHandle(myProcessBar.Increase);myProcessBar.Show();myProcessBar=null;}privatevoidScanFile(){StreamReaderaFile=newStreamReader(path1,System.Text.Encoding.Default);Stringname=Path.GetFileName(path1);Stringrole1=@"绝.*密";Regexreg1=newRegex(role1);MethodInvokermi=newMethodInvoker(ShowProcessBar);this.BeginInvoke(mi);Thread.Sleep(1000);objectobjReturn=null;while(aFile.Peek()>=0){Thread.Sleep(1);objReturn=this.Invoke(this.myIncrease,newobject[]{2});stringline=aFile.ReadLine();if(reg1.IsMatch(line)){MessageBox.Show("该文档涉密",name);return;}}MessageBox.Show("该文档不涉密",name);}privatevoidbutton1_Click(objectsender,EventArgse){OpenFileDialogofd1=newOpenFileDialog();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;}}privatevoidtextBox2_TextChanged(objectsender,EventArgse){}publicvoidbutton2_Click(objectsender,EventArgse){ThreadStartscanStart=newThreadStart(ScanFile);Threadscaning=newThread(scanStart);scaning.Name=path1;scaning.IsBackground=true;scaning.Start();}privatevoidForm1_Load(objectsender,EventArgse){}}}
谢谢大家乐
解决方案
解决方案二:
是不是代码哪地方写错了,仔细检查检查!
解决方案三:
自己顶一个,真的是有问题不会,不是自身代码敲错的问题,大神们帮我看看吧
解决方案四:
大哥,发的代码加点注释呀,读的太费劲了privatevoidShowProcessBar(){myProcessBar=newForm2();myIncrease=newIncreaseHandle(myProcessBar.Increase);myProcessBar.Show();myProcessBar=null;}
问题出在这
解决方案五:
Form2A=newForm2();第一个Form2是个类型定义A是指针(存类型分配的内存的首地址)newForm2()是让系统分配一块内存A=newForm2()就是把这块内存的地址存在A的变量里,你就可以用了——————————————————————A=newForm2();A=newForm2();A=newForm2();感觉是一样的吧,但三个newForm2()不是一个东西,是三块内存。上面三行执行完后,A里存的是最后的那块内存的地址。所以你的更新全在最后的那个窗口里
解决方案六:
改一下privateForm2myProcessBar=null;--这个不行privateDictionary<string,Form2>myProcessBar=newDictionary<string,Form2>();privateDictionary<string,IncreaseHandle>myIncrease=newDictionary<string,IncreaseHandle>();privatevoidShowProcessBar(stringname){myProcessBar[name]=newForm2();myIncrease[name]=newIncreaseHandle(myProcessBar[name].Increase);myProcessBar[name].Show();}privatevoidScanFile(){StreamReaderaFile=newStreamReader(path1,System.Text.Encoding.Default);Stringname=Path.GetFileName(path1);MethodInvokermi=newMethodInvoker(ShowProcessBar);//这块加参数,把name传进去//…………………………………………while(aFile.Peek()>=0){Thread.Sleep(1);objReturn=this.Invoke(this.myIncrease[name],newobject[]{2});//这块有变动stringline=aFile.ReadLine();if(reg1.IsMatch(line)){MessageBox.Show("该文档涉密",name);return;}}MessageBox.Show("该文档不涉密",name);}
大概是这样,你试一下