大家好,假设有一个数组buffer[],通过方法func_in()向这个数组写入数据,然后通过func_out()读取数组中的数据,我现在实现的就是通过func_in()一次性的把所有数据写入数组,然后才调用func_out()读取数组中的数据,我现在想让func_in()不断地执行,在func_in()执行2秒之后,func_out()开始不断地从数组中取数据,假设func_in()写入的速度和func_out()读取的速度一致,应该怎么实现,望大家给一个思路,谢谢!

解决方案 »

  1.   

    System.Threading.Thread.Sleep(2000); // 延迟 2000 毫秒
      

  2.   


    while (true)
    {
      // func_in()
      // 如果不是多线程 没必要 停2秒
      // func_out()
    }
      

  3.   

    以下是随手输的伪代码,思路很简单,就是一个Timer就OK了
    Public Byte[] SaveSomething; //原始数组
    int i = 0 ; //记录用来跟2取余//写入,简单的直接New一个
    private void func_in()
    {
       SaveSomething = new Byte[3]{0x1,0x2,0x3};
    }//读取
    private void func_out()
    {
      foreach(byte b in SaveSomething)
      {
       //read b
      }
    }private void Main()
    {
      Timer a = new Timer();
      a.Invatal = 2000;
      a.Tick();
    }proviate void a_Tick()
    {
     i++;
     func_in();
     if(i%2 == 0 & i!=0)func_Out(); //不等于且除2取余=0时激活func_Out()
    }
      

  4.   

    加个延迟就行
    System.Threading.Thread.Sleep
      

  5.   

    线程加timer应该能搞定了   
      

  6.   


           public void LostTime()
            {
                long beginTime = DateTime.Now.Ticks;
                while (true)
                {
                    long endTime = DateTime.Now.Ticks;
                    if ((endTime - beginTime) > 1e8) break;
                }
                return;
            }在需要延时的地方加上类似函数就可以
      

  7.   

    System.Threading.Thread.Sleep()
    延迟Sleep()
      

  8.   

    新手凑热闹:)    class Sample
            {
            bool IsRunning = false;
            buffer[] BufferProp = new buffer[Capacity];
            private void func_in()
                {
                BufferProp = InputData();
                Console.WriteLine("Input action processed.");
                }
            private void func_out()
                {
                OutputDevice = BufferProp;
                Console.WriteLine("Output action processed.");
                }
            void Action(object source, ElapsedEventArgs e)
                {
                func_out();
                func_in();
                }
            public void Start()
                {
                IsRunning = true;
                if (IsRunning)
                    {
                    Timer timer = new Timer(2 * 1000);
                    timer.Elapsed +=new ElapsedEventHandler(Action);
                    timer.Start();
                    }
                }
            }
      

  9.   

    又读了一遍题我想我误解了楼主的意思,所以又重写了一个算法:
    注释部分注释掉可以直接测试。    class Sample
            {
            bool IsRunning = false;
            bool IsInputting = false;
            bool IsOutputting = false;
            //buffer[] BufferProp = new buffer[Capacity];
            private void func_in()
                {
                //BufferProp = InputData();
                Console.WriteLine("Input action processed.");
                }
            private void func_out()
                {
                //OutputDevice = BufferProp;
                Console.WriteLine("Output action processed.");
                }
            void SwitchStatus(object source, ElapsedEventArgs e)
                {
                IsInputting = !IsInputting;
                IsOutputting = !IsInputting;
                while (IsInputting)
                    func_in();
                while (IsOutputting)
                    func_out();
                }
            public void Start()
                {
                IsRunning = true;
                if (IsRunning)
                    {
                    IsInputting = true;
                    Timer timer = new Timer(2 * 1000);
                    timer.Elapsed += new ElapsedEventHandler(SwitchStatus);
                    timer.Start();
                    }
                }
            }
      

  10.   

    System.Threading.Thread.Sleep应该可以
      

  11.   

    用一个timer控件,两秒钟后执行, 在func_in() 方法中激活 timer 就可以了
      

  12.   

    假设func_in()写入的速度和func_out()读取的速度一致
    ----可以使用队列,一个线程不断的往队列中写数据一个线程不断取数据(队列先进先出),注意在多线程对队列操作时需要加锁,让它在读和写的某一时刻独占队列
      

  13.   

    using System;
    using System.Threading;
    class Sample
    {
        bool IsRunning = false;    private void func_in()
        {
            //BufferProp = InputData();
            Console.WriteLine("Input action processed.");
        }
        private void func_out()
        {
            //OutputDevice = BufferProp;
            Console.WriteLine("Output action processed.");
        }    public void MainThread()
        {
            while (true)
            {
                Thread th = new Thread(new ThreadStart(func_in));//读数据线程
                th.Start();
                Thread.Sleep(2000);//2秒后启动新线程收数据
                th = new Thread(new ThreadStart(func_out));//读数据线程
                th.Start();
            }
        }    public void Start()
        {
           
            if (!IsRunning)
            {
                //主线程
                Thread th = new Thread(new ThreadStart(MainThread));
                th.Start();
                IsRunning = true;
            }
         
        }
    }
      

  14.   

    用线程的Sleep会出现程序假死的现象吧!C#里面好像有一个专门的函数吧!具体是哪个给忘了。