我在Form1中有这个功能,这很好,这个想法是从一个大的XML文件中检索一些文本。所以在Form1构造中我做了:如何在BackgroundWorker中运行类方法时以不同形式重用类?
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);
}
这是按钮点击事件在那里我开始BackgroundWorker的:
private void button8_Click(object sender, EventArgs e)
{
backgroundWorker2.RunWorkerAsync();
}
在新的类中,我创建了一个新的函数public function,它获得一些变量m 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)
{
我的问题是如何改变和测试()函数转换从Form1以新的类到新的功能,所以它也将工作,并与backgroundworker2使用?
我试图使用/添加FileStream那里,并添加whie循环将逐行阅读,但它没有工作好。
**这与功能的新类有至极dosent行为,因为它是测试()在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)
{
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的新类和函数中做了一件乱七八糟的事情。 **
所有这些事情都可以作为解开你想要的。后台工作者是一个类实例,你可以在任何地方定义它。 Filestream对象是相同的。只要他们可以在应用程序(公共)内看到对方,就可以调用它们。所以当你说“它没有工作好”时,问题究竟是什么? –
当我说:“这没有工作好”我的意思是,当它只是在Form1中不使用它工作,我想它的工作backgroundworker2。但只要我把它移到新课堂,它就提取了我不想要的moew字符串。我的意思是它提取了更多的标签,而不仅仅是来自xml的文本。而在功能IM新类使用可变串读至极是字符串型F在Form1并在Form1上串f的阅读到最后大的XML文件,但在新的类和函数IM也使用的FileStream至极也阅读本大xml文件。然后我将这些变量混合到新类中。 – user1363119
我将添加到我的帖子功能和代码,因为它现在在新的类。它不会给我错误,但它的剂量正常工作,因为我需要。 – user1363119