要睡觉去了,你lock了,肯定不可能实现了,任何一个线程逮住run都给你执行彻底了

解决方案 »

  1.   

    用sleep和waitforsingleobject来实现还差不多
      

  2.   


           List<Thread> t = new List<Thread>();//保存线程
            //输出变量0~30,必须是静态,否则每个线程一个是无法保证0~30顺序输出的
            static int outputIndex = 0;
           //同步对象,保证ouputIndex自增和输出的同步性
           static object syncLock = new object();        static void Main(string[] args)
            {
                Program p = new Program();
                p.Start();
                Console.ReadLine();
            }        public Program()
            {
                int threadCount = 6;
                for (int i = 0; i < threadCount; i++)
                {
                    Thread therad = new Thread(Run);
                    therad.Name = i.ToString();
                    t.Add(therad);
                }
            }
            public void Start()
            {
                foreach (Thread tt in t)
                {
                    tt.Start();
                }
            }
            public void Run()
            {
                try
                {
                        while (true)
                        {
                            lock (syncLock)
                                {
                                    if (outputIndex < 30)
                                    {   
                                        outputIndex++;
                                        Console.WriteLine("{0}:{1}", Thread.CurrentThread.Name, outputIndex);
                                        //不加Sleep单个线程就可以完成全部输出,
                                        //为了体现为多线程的效果需要适当的Sleep
                                        Thread.Sleep(1);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                 }
                        }
                }
                catch
                {
                    
                }
                //后面的Stop毫无意义,已删除
            }
      

  3.   

    给你用双线程写个例子:(我觉得这样做实在是没有必要)using System;using System.Collections.Generic;using System.Text;using System.Threading; namespace ConsoleApplication1{    delegate int AddValueDele(object o);     class Program    {        public Program()        {            Value v=new Value();             AddValueDele a1 = new AddValueDele(AddValue);            AddValueDele a2 = new AddValueDele(AddValue);             while (v.number < 5)            {                IAsyncResult result = a1.BeginInvoke(v, null, null);                if (!result.IsCompleted)                {                    Console.WriteLine("a1 invoked.");                }                Console.WriteLine(a1.EndInvoke(result) );                 if (v.number < 5)                {                    IAsyncResult result2 = a2.BeginInvoke(v, null, null);                    if (!result2.IsCompleted)                    {                        Console.WriteLine("a2 invoked.");                    }                    Console.WriteLine(a2.EndInvoke(result2));                }            }        }         public int AddValue(object o)        {            Value v = (Value)o;            v.number++;            Thread.Sleep(2000);            //Console.WriteLine("number:{0}", v.number);            return v.number;        }         static void Main(string[] args)        {            Program p1 = new Program();            //Console.WriteLine( SetNumber(-2) );            Console.ReadLine();        }    }      public class Value    {        public int number=0;    }}
    你可以运行一下,然后再考虑你的codes
      

  4.   

    呵呵  谢谢大家的支持啊  那个时候 急着弄所以很多地方发了贴 多数地方看了后  有点失望 居然把这个论坛  忘记了....
       不过现在才知道答案啊  4楼的 运行没问题  我第一次发的代码也是复制人家的 有点明白 又不是很明白  但是那个代码在WINFROM编程的时候  会出问题  4楼那个 我测试过没有问题...
      6楼发的 我也看了  不过还真是晕了  效果我也不知道是不是 因为看代码的时候 看了一般就没信心了 居然用了  IAsyncResult  也许是比较好  但是确实是太麻烦了  更何况没注释就没看了  对不起了 浪费你的劳动成果了