gpt4 book ai didi

c# - 如何限制在C# Selenium 中运行的线程

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:55 25 4
gpt4 key购买 nike

我有这个代码:

foreach (var s in linesTxt)
{
if (!myList.Contains(s))
{
myList.Add(s);
if (!String.IsNullOrWhiteSpace(s))
{
Processor(s);
}
}
}

它获取单个 .txt 文件中的所有详细信息,每行一个数据。

这是线程部分:

public void Processor(string Sample)
{
//Process p = Process.GetCurrentProcess();
ThreadStart Thread1 = new ThreadStart(() => firstFunc(Sample));
ThreadStart Thread2 = new ThreadStart(() => secondFunc(Sample));
ThreadStart Thread3 = new ThreadStart(() => thirdFunc(Sample));
ThreadStart Thread4 = new ThreadStart(() => fourthFunc(Sample));
ThreadStart Thread5 = new ThreadStart(() => fifthFunc(Sample));
ThreadStart Thread6 = new ThreadStart(() => sixthFunc(Sample));
ThreadStart Thread7 = new ThreadStart(() => seventhFunc(Sample));
ThreadStart Thread8 = new ThreadStart(() => eightFunc(Sample));
ThreadStart Thread9 = new ThreadStart(() => ninthFunc(Sample));
ThreadStart Thread10 = new ThreadStart(() => tenthFunc(Sample));
ThreadStart Thread11 = new ThreadStart(() => eleventhFunc(Sample));
ThreadStart Thread12 = new ThreadStart(() => twelveFunc(Sample));
ThreadStart Thread13 = new ThreadStart(() => thirteenFunc(Sample));
Thread[] mainThread = new Thread[13];
mainThread[0] = new Thread(Thread1);
mainThread[1] = new Thread(Thread2);
mainThread[2] = new Thread(Thread3);
mainThread[3] = new Thread(Thread4);
mainThread[4] = new Thread(Thread5);
mainThread[5] = new Thread(Thread6);
mainThread[6] = new Thread(Thread7);
mainThread[7] = new Thread(Thread8);
mainThread[8] = new Thread(Thread9);
mainThread[9] = new Thread(Thread10);
mainThread[10] = new Thread(Thread11);
mainThread[11] = new Thread(Thread12);
mainThread[12] = new Thread(Thread13);
foreach (Thread myThread in mainThread)
{
//ThreadPool.SetMaxThreads(26, 26);
//p.Refresh();
//Console.WriteLine("Before start: {0}", p.Threads.Count);
myThread.Start();
}
}

firstFunc, secondFunc .... 包含 selenium c# 代码。

注意:我已经尝试使用 ThreadPool.SetMaxThreads 但没有成功。

例如,如果 txt 文件包含 20 个数据,那么我的电脑将不会响应,但如果它只包含 5 个或更少,那么即使我遇到一些延迟,它也能正常工作。

我想要的是能够处理txt文件中任意数量的数据。

已更新

firstFunc 包含这个:

                    var driverService1 = PhantomJSDriverService.CreateDefaultService();
driverService1.HideCommandPromptWindow = true;
var driver1 = new PhantomJSDriver(driverService1);
driver1.Navigate().GoToUrl("http://sample.com");
driver1.FindElement(By.Id("urlInput")).SendKeys(Domain);
driver1.FindElement(By.ClassName("convert-url")).Click();
var wait1 = new WebDriverWait(driver1, TimeSpan.FromSeconds(10));
wait1.Until(ExpectedConditions.ElementExists(By.XPath("//*[@class='alert alert-success alert-dismissible fade in']//p//input")));
System.Threading.Thread.Sleep(2000);
val1 = driver1.FindElement(By.XPath("//*[@class='alert alert-success alert-dismissible fade in']//p//input")).GetAttribute("value");
if (Results.InvokeRequired)
{
Results.Invoke(new MethodInvoker(delegate { Results.Items.Add(DomainSearch + " => " + "http://sample.com => " + val1); }));
}
driver1.Quit();
driverService1.Dispose();

最佳答案

你可以简单地使用 Parallel :

var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;

Parallel.Invoke(options,
() => firstFunc(Sample),
() => secondFunc(Sample),
() => thirdFunc(Sample),
...
);

或者Task.Factory:

ThreadPool.SetMaxThreads(5, 1000);

Task[] tasks = new Task[3] {
Task.Factory.StartNew(() => firstFunc(Sample)),
Task.Factory.StartNew(() => secondFunc(Sample)),
Task.Factory.StartNew(() => thirdFunc(Sample)),
...
};

Task.WaitAll(tasks);

关于c# - 如何限制在C# Selenium 中运行的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com