我有做了个扫IP的程序 发现单线程扫描的话非常慢
于是我想让用户自行设置线程的个数
做法是这样的 比如有4个线程 第一个线程 从1开始,......第四个线程从4开始 ,每次循环的增量就是线程的个数
线程的入口函数以及其显示的函数都一样 , 现在问题是我该如何从入口函数里设置起始值,并将结果返回到窗体控件上(线程的个数不确定,是用户自行设置的)
于是我想让用户自行设置线程的个数
做法是这样的 比如有4个线程 第一个线程 从1开始,......第四个线程从4开始 ,每次循环的增量就是线程的个数
线程的入口函数以及其显示的函数都一样 , 现在问题是我该如何从入口函数里设置起始值,并将结果返回到窗体控件上(线程的个数不确定,是用户自行设置的)
List<Thread> _scaners=new List<Thread>();void Start(int threadCount){
Thread t;
for(int i=0;i<threadCount;i++){
t=new Thread(new ThreadStart(Scan));
t.Start();
_scaners.add(t);
}
}void Scan(){
// 扫描
}
//借用1楼
static int countThread = 0;void Start(int num)
{
Thread t;
for(int i=0;i<threadCount;i++)
{
t=new Thread(new ThreadStart(Scan));
t.Start();
countThread++;
}
}static void Scan()
{
// 扫描
}
void Scan(){
// 扫描
} 就是这里,如何设置扫描的起始的值,因为每个线程扫描的超始值都是不同的,
int threadCount=10;
for(int i=0;i<threadCount;i++)
{
listThread.Add(new Thread(new ThreadStart(new Class1(i+1,threadCount).Scan)));
listThread[i].Start();
}Class Class1
{
private int startNumber;
private int threadCount;
public class1(int startNumber,int threadCount)
{
this.startNumber=startNumber;
this.threadCount=threadCount;
}
public void Scan()
{
for(int i=startNumber;i<255;i=i+threadCount)
{
//Do Ping Method
}
}
}
using System;
using System.Threading;class CalculateTest
{
static void Main()
{
Calculate calc = new Calculate();
Console.WriteLine("Result = {0}.",
calc.Result(234).ToString());
Console.WriteLine("Result = {0}.",
calc.Result(55).ToString());
}
}class Calculate
{
double baseNumber, firstTerm, secondTerm, thirdTerm;
AutoResetEvent[] autoEvents;
ManualResetEvent manualEvent; // Generate random numbers to simulate the actual calculations.
Random randomGenerator; public Calculate()
{
autoEvents = new AutoResetEvent[]
{
new AutoResetEvent(false),
new AutoResetEvent(false),
new AutoResetEvent(false)
}; manualEvent = new ManualResetEvent(false);
} void CalculateBase(object stateInfo)
{
baseNumber = randomGenerator.NextDouble(); // Signal that baseNumber is ready.
manualEvent.Set();
} // The following CalculateX methods all perform the same
// series of steps as commented in CalculateFirstTerm. void CalculateFirstTerm(object stateInfo)
{
// Perform a precalculation.
double preCalc = randomGenerator.NextDouble(); // Wait for baseNumber to be calculated.
manualEvent.WaitOne(); // Calculate the first term from preCalc and baseNumber.
firstTerm = preCalc * baseNumber *
randomGenerator.NextDouble(); // Signal that the calculation is finished.
autoEvents[0].Set();
} void CalculateSecondTerm(object stateInfo)
{
double preCalc = randomGenerator.NextDouble();
manualEvent.WaitOne();
secondTerm = preCalc * baseNumber *
randomGenerator.NextDouble();
autoEvents[1].Set();
} void CalculateThirdTerm(object stateInfo)
{
double preCalc = randomGenerator.NextDouble();
manualEvent.WaitOne();
thirdTerm = preCalc * baseNumber *
randomGenerator.NextDouble();
autoEvents[2].Set();
} public double Result(int seed)
{
randomGenerator = new Random(seed); // Simultaneously calculate the terms.
ThreadPool.QueueUserWorkItem(
new WaitCallback(CalculateBase));
ThreadPool.QueueUserWorkItem(
new WaitCallback(CalculateFirstTerm));
ThreadPool.QueueUserWorkItem(
new WaitCallback(CalculateSecondTerm));
ThreadPool.QueueUserWorkItem(
new WaitCallback(CalculateThirdTerm)); // Wait for all of the terms to be calculated.
WaitHandle.WaitAll(autoEvents); // Reset the wait handle for the next calculation.
manualEvent.Reset(); return firstTerm + secondTerm + thirdTerm;
}
}
{
private int startNumber;
public int StartNumber
{
get { return startNumber; }
set { startNumber = value; }
} public void StartScan()
{
//从startNumber处开始扫描
}
}
//.........多线程调用
ScanInfo scan1 = new ScanInfo();
scan1.StartNumber = 1; Thread th1 = new Thread(new ThreadStart(scan1.StartScan));
th1.Start();