我在 Form1 中有这个功能,效果很好。这个想法是从一个大的 xml 文件中检索一些文本。所以在 Form1 构造中我做了:
r = new StreamReader(@"D:\Deponia_Work\Deponia Extracted Files\000004aa.xml");
f = r.ReadToEnd();
r.Close();
变量 f 是字符串。
现在我在 Form1 中有这个函数,我调用了 test(),它正在从 000004aa.xml 大文件中检索/提取文本。
private void test()
{
int countFiles = 0;
byte[] a;
string startTag = "T256=\"";
string endTag = "\"";
int index = 0;
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
int fileLength = f.Length;
w = new StreamWriter(@"d:\testingdeponias.txt");
while (true)
{
if (index > f.LastIndexOf(startTag))
{
break;
}
int startTagIndex = f.IndexOf(startTag, index);
int stringIndex = startTagIndex + startTagWidth;
index = stringIndex;
int endTagIndex = f.IndexOf(endTag, index);
int stringLength = endTagIndex - stringIndex;
if (stringLength == 0)
{
}
else
{
string test = f.Substring(stringIndex, stringLength);
if (test.Contains("<pa>"))
{
string t = "<pa>";
int y = t.Length;
string test1 = test.Substring(0, stringLength - y);
listBox1.Items.Add(test1);
w.WriteLine(test1);
}
//else
// {
listBox1.Items.Add(test);
w.WriteLine(test);
// }
}
}
w.Close();
}
然后我在 Form1 中添加了一个新的 backgroudnworker2 事件:
这是 DoWork 事件:
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker2 = sender as BackgroundWorker;
lists1.extractTextDeponia(worker2, backgroundWorker2, listBox1, textBox1,f, w, e);
}
这是我启动后台工作程序的按钮单击事件:
private void button8_Click(object sender, EventArgs e)
{
backgroundWorker2.RunWorkerAsync();
}
在新类中,我创建了一个从 Form1 获取一些变量的新函数公共函数。
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.Web;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace List_Words
{
class Lists
{
public void List_Words()
{
}
public void extractTextDeponia(BackgroundWorker worker, BackgroundWorker backgroundWorker2, ListBox lbox , TextBox tbox , StreamWriter streamW , string read, DoWorkEventArgs e)
{
我的问题是如何更改并将 test() 函数从 Form1 转换为新类到新函数,以便它也可以与 backgroundworker2 一起工作和使用?
我尝试在那里使用/添加 FileStream 并添加了 while 循环,该循环将逐行读取,但效果不佳。
** 这是带有函数的新类,它的作用与 Form1 中的 test() 不同**
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.Web;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace List_Words
{
class Lists
{
public void List_Words()
{
}
public void extractTextDeponia(BackgroundWorker worker, BackgroundWorker backgroundWorker2, ListBox lbox , TextBox tbox , StreamWriter streamW , string read, DoWorkEventArgs e)
{
string startTag = "T256=\"";
string endTag = "\"";
int index = 0;
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
int fileLength = read.Length;
using (var fs = new FileStream(@"D:\Deponia_Work\Deponia Extracted Files\000004aa.xml", FileMode.Open, FileAccess.Read))
{
using (var h = new StreamReader(fs))
{
string line;
line = h.ReadLine();
while ((line = h.ReadLine()) != null)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
int percent = (int)(fs.Position * 100 / fs.Length);
backgroundWorker2.ReportProgress(percent);
if (index > read.LastIndexOf(startTag))
{
break;
}
int startTagIndex = read.IndexOf(startTag, index);
int stringIndex = startTagIndex + startTagWidth;
index = stringIndex;
int endTagIndex = read.IndexOf(endTag, index);
int stringLength = endTagIndex - stringIndex;
if (stringLength == 0)
{
}
else
{
line = read.Substring(stringIndex, stringLength);
if (line.Contains("<pa>"))
{
string t = "<pa>";
int y = t.Length;
string test1 = line.Substring(0, stringLength - y);
if (lbox.InvokeRequired)
{
lbox.Invoke(new MethodInvoker(delegate { lbox.Items.Add(test1); }));
}
//w.WriteLine(test1);
line = h.ReadLine();
if (tbox.InvokeRequired)
{
tbox.Invoke(new MethodInvoker(delegate { tbox.Text = test1; }));
}
}
//else
// {
if (lbox.InvokeRequired)
{
lbox.Invoke(new MethodInvoker(delegate { lbox.Items.Add(line); }));
}
//w.WriteLine(line);
if (tbox.InvokeRequired)
{
tbox.Invoke(new MethodInvoker(delegate { tbox.Text = line; }));
}
// }
}
}
}
}
}
streamW = new StreamWriter(@"d:\testingdeponias.txt");
streamW.AutoFlush = true;
streamW.Write(read);
streamW.Close();
}
}
}
在 form1 中,我将其称为:
BackgroundWorker worker2 = sender as BackgroundWorker;
lists1.extractTextDeponia(worker2, backgroundWorker2, listBox1, textBox1,w, f, e);
w 是流编写器 f 是字符串,e 是 backgroundowrker2 DoWork e 变量 正如我上面所展示的,我在 Form1 构造函数中使用 f 来读取大的 xml 文件,但我也在新类中执行它,并且使用 FileStream 函数真是一团糟。**