我有这段代码:
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();
答案 0 :(得分:1)
您可以简单地使用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);