我有做了个扫IP的程序 发现单线程扫描的话非常慢
  于是我想让用户自行设置线程的个数
    做法是这样的 比如有4个线程      第一个线程 从1开始,......第四个线程从4开始  ,每次循环的增量就是线程的个数
    
线程的入口函数以及其显示的函数都一样 ,  现在问题是我该如何从入口函数里设置起始值,并将结果返回到窗体控件上(线程的个数不确定,是用户自行设置的)

解决方案 »

  1.   


    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(){
        // 扫描
    }
      

  2.   

    返回窗体要用委托,更新UI必须在UI线程里
      

  3.   


    //借用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()
    {
        // 扫描
    }
      

  4.   

     可能我的问题没有描述清楚  我的问题不是如何设置线程,而是线程入口函数的设置
    void Scan(){
        // 扫描
    }  就是这里,如何设置扫描的起始的值,因为每个线程扫描的超始值都是不同的,
      

  5.   

        List<Thread> listThread = new List<Thread>();
        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
             }
        }
    }
      

  6.   

    通过构造函数在ThreadStart里传递参数,自己想的,基本上没问题。应该还有更好的办法还没想到
      

  7.   

    ThreadPool.QueueUserWorkItem (WaitCallback, Object) 
      

  8.   

    采用事件AutoResetEvent are=new AutoResetEvent(false);MSDN:
    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;
        }
    }
      

  9.   

    较为简单可行的方法是,把扫描方法,封装成一个类.并定义一个属性,设置初始值,如:public class ScanInfo
        {
            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();
      

  10.   

    接收的参数中是否有unicode字符?