如何限制在C#Selenium中运行的线程
本文关键字:运行 线程 C#Selenium 何限制 | 更新日期: 2023-09-27 17:59:39
我有这个代码:
foreach (var s in linesTxt)
{
if (!myList.Contains(s))
{
myList.Add(s);
if (!String.IsNullOrWhiteSpace(s))
{
Processor(s);
}
}
}
它获取单个.txt文件中的每个Details,每行一个数据。
这是线程部分:
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文件中处理任何数量的数据。
更新
第一个Func包含以下内容:
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);