最小化Windows

时间:2015-10-19 14:27:39

标签: c#

该程序使用两个线程:一个用于从USB收集数据,另一个用于写入/显示数据。它似乎按预期工作,但是,只要我最小化/恢复PC上的任何窗口,或按下窗体上的任何按钮,它就会停止正常工作(它会停止接收数据)。我使用链接列表来保持数据流顺序。我怀疑由于变量声明不正确,链接可能会丢失。我的代码中是否存在可能导致此问题的明显错误?

namespace Streamer
{
    public class Form1 : System.Windows.Forms.Form
    {

        Thread tListen;
        Thread tDisplay;

        public class MY_DATA_BUFFER
        {            
            public byte[] buffer;
            public int length;
            public MY_DATA_BUFFER nextBuff;
        }

        private Object thisLock = new Object();
        MY_DATA_BUFFER pHead = null;
        private static AutoResetEvent DataQueueEvent = new AutoResetEvent(false);

        public Form1()
        {
            // some settings;
        }

        public unsafe void dataDisplayThread()
        {
             MY_DATA_BUFFER pWorkingSet = pHead;

             if (pWorkingSet == null)
             {
                  // Make sure link list head is initialized...........
                  do {DataQueueEvent.WaitOne();} while (pHead == null);

                  // Wait till the two datas are available to write.
                  if (pHead.nextBuff == null){ DataQueueEvent.WaitOne(); }

                  if (pWorkingSet == null) { pWorkingSet = pHead; }
             }

             // Let's start the data write loop 
             while (bRunning || (pWorkingSet != null))
             {
                  // copy small array to a bigger array
                  for (int i = 0; i < pWorkingSet.length; i++)
                  {
                      pixelValues[pixptr] = pWorkingSet.buffer[i];
                      pixptr++;
                  }

                 if (pixptr >= imWidth * imHeight)
                 {
                      pixptr = 0;
                      // show data in pixelValues
                 }

                 // Traverse through the link list data structure.
                 if (pWorkingSet.nextBuff == null)
                 {
                    do
                    {
                        if (pWorkingSet.nextBuff == null DataQueueEvent.WaitOne();

                        lock (thisLock)
                        {
                            if (pWorkingSet.nextBuff == null && !bRunning)
                            break;
                        }
                     } while (pWorkingSet.nextBuff == null);
                 }

                 // We are good to loop for the next operation
                 lock (thisLock)
                 {
                     pHead = pHead.nextBuff;
                     pWorkingSet = pHead;
                 }
             }
             // All write operation is at completion
             pHead = null;
         }

         public unsafe void XferData(...)
         {
             MY_DATA_BUFFER tempBuff = null;

             for (; bRunning; )
             {
                // .... do something 
                // push the data to a link list for
                lock (thisLock)
                {
                    MY_DATA_BUFFER newBuff = new MY_DATA_BUFFER();
                    newBuff.nextBuff = null;
                    newBuff.buffer = xBufs[k];
                    newBuff.length = len;

                    if (tempBuff == null) tempBuff = newBuff;
                    else
                    {
                        tempBuff.nextBuff = newBuff;
                        tempBuff = newBuff;
                    }

                    if (pHead == null) pHead = newBuff;
                    else
                    {
                        DataQueueEvent.Set();
                    }
                }
                ///////////////////Link List Population completes///////////
                Thread.Sleep(0);
             } // End infinite loop                      
         }

         private void StartBtn_Click(object sender, System.EventArgs e)
         {

            if (StartBtn.Text.Equals("Start"))
            {
                // ...
                pHead = null;
                bRunning = true;

                tListen = new Thread(new ThreadStart(XferThread));
                tListen.IsBackground = true;
                tListen.Priority = ThreadPriority.Highest;
                tListen.Start();

                tDisplay = new Thread(new ThreadStart(dataDisplayThread));
                tDisplay.IsBackground = true;
                tDisplay.Priority = ThreadPriority.Highest;
                tDisplay.Start();
            }
            else
            {
                if (tListen.IsAlive)
                {
                    // ...
                    bRunning = false;
                    if (tListen.Join(5000) == false )
                    tListen.Abort();
                    tListen = null;
                }
                if (tDisplay.IsAlive)
                { 
                    if (tDisplay.Join(5000) == false )
                         tDisplay.Abort();

                    Display = null;
                }
            }
         }
     }
 }

0 个答案:

没有答案