2012-05-03 74 views
0

我在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的新类和函数中做了一件乱七八糟的事情。 **

+0

所有这些事情都可以作为解开你想要的。后台工作者是一个类实例,你可以在任何地方定义它。 Filestream对象是相同的。只要他们可以在应用程序(公共)内看到对方,就可以调用它们。所以当你说“它没有工作好”时,问题究竟是什么? –

+0

当我说:“这没有工作好”我的意思是,当它只是在Form1中不使用它工作,我想它的工作backgroundworker2。但只要我把它移到新课堂,它就提取了我不想要的moew字符串。我的意思是它提取了更多的标签,而不仅仅是来自xml的文本。而在功能IM新类使用可变串读至极是字符串型F在Form1并在Form1上串f的阅读到最后大的XML文件,但在新的类和函数IM也使用的FileStream至极也阅读本大xml文件。然后我将这些变量混合到新类中。 – user1363119

+0

我将添加到我的帖子功能和代码,因为它现在在新的类。它不会给我错误,但它的剂量正常工作,因为我需要。 – user1363119

回答

0

有一件事可能会帮助你改变你的编程思维。不要考虑表单和事件,而要考虑这些数据实体上的数据实体和操作,然后可以使用服务类型模式。

所以你要做的是创建一个服务类,它可以做任何你想要发生的事情。由于您使用的是后台工作人员,因此我会将其创建为实例类而不是静态类。

public class FileWorkerService 
{ 
    private BackgroundWorker _worker; 

    public FileWorkerService() 
    { 
     _worker = new BackgroundWorker(); 
    } 

    public void ReadFileAndProcessIt() 
    { 
     if (!_worker.IsBusy) 
     { 
      _worker.RunWorkerAsync(); 
     } 
    } 
} 

现在,刚丝了_worker你与你的事件之前的方式一样,你的服务类是准备做的工作。

为了让它做些什么,你只需创建它的一个实例并调用启动worker的函数。

所以在你的表单按钮,你会有...

private void button8_Click(object sender, EventArgs e) 
     { 
      var newService = new FileWorkerService(); 
      newService.ReadFileAndProcessIt(); 
     } 

=================编辑=================== =============== - 增加了如何从服务中更新后台工作的进展情况。 < < < ============================================== ============

里面你FileWorkerService类

public void SetWorkerProgress(int currentValue) 
{ 
    _worker.ReportProgress(currentValue); 
} 

给类以外的来电,您的主要形式,你会怎么做?

newService.SetWorkerProgress(whatever); 
+0

David如何更改/使用test()函数与backgroundworker一起使用?可以说,我不测试()函数改变任何东西,因为它是在Form1中,但我需要以某种方式从变量传递给BackgroundWorker的例如backgroundWorker2.ReportProgress(百分比);我以前用它与FileStream,但让我说我不想改变测试()函数中的任何东西,我如何报告给背景工作? – user1363119

+0

由于后台工作人员属于您创建的服务类,您的服务类将成为管道。你可以创建一个访问器,这样你就可以直接将后台工作者接触到你的服务类,或者你可以创建一个方法来做你想要的后台工作者。我真的不能格式化的内容在这里,所以我会编辑 –

+0

你遇到麻烦的服务工人做你想要它做的事情,或者是你遇到麻烦服务人员报告其值回形式后一次和整个作业的处理之前显示它(常与长时间运行的进程和WinForms打交道时的一个问题) –

相关问题