0

我有这个类,稍后我将在 Form1 中为它创建一个实例,将变量从 Form1 传递给类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GatherLinks
{
    class WebcrawlerConfiguration
    {

        public string url;
        public int levels;
        public string mainurl;
        public Dictionary<string,List<string>> localy;
        public bool removeext;
        public bool downloadcontent;
        public int failedUrls;
        public bool toCancel;
        public System.Threading.ManualResetEvent busy;

        public WebcrawlerConfiguration()
        {

        }
    }
}

我如何让这个类从 Form1 获取变量?当我为类创建实例时,类中的每个变量都应该获取 Form1 中的变量。

然后我有这个类:

class BackgroundWebCrawling
    {
        int counter = 0;
        List<string> WebSitesToCrawl;
        BackgroundWorker mainBackGroundWorker;
        BackgroundWorker secondryBackGroundWorker;
        WebCrawler webcrawler1;
        WebcrawlerConfiguration webcrawlerCFG;
        public BackgroundWebCrawling()
        {
            mainBackGroundWorker = new BackgroundWorker();
            mainBackGroundWorker.DoWork += mainBackGroundWorker_DoWork;
        }

        private void mainBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < WebSitesToCrawl.Count; i++)
            {
                if (counter >= 3)
                {
                    Thread.Sleep(10);
                }
                else
                {
                    webcrawler1 = new WebCrawler(webcrawlerCFG);
                    counter++;
                    secondryBackGroundWorker = new BackgroundWorker();
                    secondryBackGroundWorker.DoWork += secondryBackGroundWorker_DoWork;
                }


            }            
        }

        private void secondryBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {

                if (offlineOnline == true)
                {
                    offlinecrawling(f, levelsToCrawl, e);
                }
                else
                {
                    webcrawler1.webCrawler(
                }
                counter--;
        }

在我做的底部: webcrawler1.webCrawler( 函数 webCralwer 应该从第一个类 WebcrawlerConfiguration 获取所有变量。

这就是 WebCrawler 函数的样子:

public List<string> offlinecrawling(string url, int levels, string mainurl, Dictionary<string, List<string>> localy, bool removeext, bool downloadcontent, int failedUrls, bool toCancel, System.Threading.ManualResetEvent busy)

所以现在当我在做 webcrawler1.webCrawler( 它要求所有的变量......但我想以某种方式从 WebcrawlerConfiguration 传递它们。

我该怎么做 ?

4

0 回答 0