private void a(int i)
{}我想用多个线程同时调用上面的函数。并且I的值不同。如何实现??

解决方案 »

  1.   

    拿一个函数把你要的封装一下
    private void aExtend()
    {
       a(10);
    }
      

  2.   

    class myClass
    {   
           int i=0;
          public myclass(int j)
          {
             i=j;
           }
           private void a(int i)
           {        }}
    不同的线程声明不同的实例
      

  3.   

    对于二楼的朋友的回答:那么我有6个线程需要调用A函数,就需要用6个函数封装吗?因为我的I是从0--6的。。对于三楼的朋友的回答:您的意思是不是让我令建立一个类,然后在主程序中,建立这个类的对象。每个线程都这样做? 那么,具体代码怎么写?? 会不会导致I值的冲突??
      

  4.   

    可以试试这个:public Thread (
    ParameterizedThreadStart start
    )
    using System;
    using System.Threading;public class Work
    {
        public static void Main()
        {
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate.
            //
            Thread newThread = new Thread(
                new ParameterizedThreadStart(Work.DoWork));
            
            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any 
            // reference type or value type. The following code passes
            // the integer value 42.
            //
            newThread.Start(42);        // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ParameterizedThreadStart delegate.
            //
            Work w = new Work();
            newThread = new Thread(
                new ParameterizedThreadStart(w.DoMoreWork));
            
            // Pass an object containing data for the thread.
            //
            newThread.Start("The answer.");
        }
     
        public static void DoWork(object data)
        {
            Console.WriteLine("Static thread procedure. Data='{0}'",
                data);
        }    public void DoMoreWork(object data)
        {
            Console.WriteLine("Instance thread procedure. Data='{0}'",
                data);
        }
    }/* This code example produces the following output (the order 
       of the lines might vary):Static thread procedure. Data='42'
    Instance thread procedure. Data='The answer'
    */这个构造函数是允许带参的...
      

  5.   

    这是MSDN上的一个很简单的例子,看看就明白了...
    当然这要求你的VS是2005的,不然也只能像楼上几位说的,封装在类里了...具体见:
    ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref12/html/C_System_Threading_Thread_ctor_1_e41c9448.htm
      

  6.   

    那么 object 如何转换成 int 型
      

  7.   

    TO:
    那么 object 如何转换成 int 型如果你要传的参数是int型的,那就直接用int型的作参数不就行了..public static void DoWork(int data)
        {
             //....
        }
      

  8.   

    直接用int 不行,不过我用(int)已经可以强制转换成int型了。现在有一个问题,我6个线程一起调用dowork(object i)会不会引起dowork函数中的变量冲突?? 例如:
    public static void DoWork(object data)
        {
             int a;
             string str
             a=(int)data
        }这个函数里,我对str有不同的操作。就是根据传来的 data 值判断的。请问多个线程同时调用这个函数,会不会使str变量的值发生冲突??
      

  9.   

    你这是局部变量,应该没什么问题...如果不放心,可以加个互斥量...Mutex mx=new Mutex();public static void DoWork(object data)
        {
             mx.WaitOne();
             int a;
             string str
             a=(int)data
             mx.ReleaseMutex();
        }
      

  10.   

    用线程池也可以
    ThreadPool.QueueUserWorkItem( new WaitCallback( new XX.a ) , i );
      

  11.   

    http://blog.csdn.net/fangxinggood/archive/2005/09/09/475891.aspx