日期:2014-05-18  浏览次数:20880 次

江湖救急这次,请各位大侠救我一命,诡异的问题,苦想几天没解决
C# code

        /// <summary>
        /// 直接发送数据没有问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {

            int iRet = -1;

            short[] PriShortChamberRecipeArray = GetRecipeDataFromXml(@"C:\test.xml");

            iRet = axActQJ71E71TCP1.WriteDeviceBlock2("R0", PriShortChamberRecipeArray.Length, ref PriShortChamberRecipeArray[0]);

        }



PriShortChamberRecipeArray就是个数组,我确定数组数据没问题,用硬件自带的开发包里面的函数WriteDeviceBlock2向硬件发送数据,如果只按一个button发送,绝对没问题


C# code

        /// <summary>
        /// 开启线程发送数据有问题
        /// </summary>
        Thread t;
        private void button4_Click(object sender, EventArgs e)
        {
            //    th.Start();   

            short[] PriShortChamberRecipeArray = GetRecipeDataFromXml(@"C:\test.xml");
            //实例化ThreadWithState类,为线程提供参数
            ThreadWithState tws = new ThreadWithState(axActQJ71E71TCP1, PriShortChamberRecipeArray);


            // 创建执行任务的线程,并执行
            t = new Thread(new ThreadStart(tws.ThreadProc));
            t.Start();
            t.Join();
        }



        //ThreadWithState 类里包含了将要执行的任务以及执行任务的方法
        public class ThreadWithState
        {
            
            //要用到的属性,也就是我们要传递的参数
            private AxACTETHERLib.AxActQJ71E71TCP _AxActQJ71E71TCP;
            private short[] _PriShortChamberRecipeArray;

            //包含参数的构造函数
            public ThreadWithState(AxACTETHERLib.AxActQJ71E71TCP AxActQJ71E71TCP, short[] PriShortChamberRecipeArray)
            {
                _AxActQJ71E71TCP = AxActQJ71E71TCP;
                _PriShortChamberRecipeArray = PriShortChamberRecipeArray;
            }

            //要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
            int iRet = 0;
            public void ThreadProc()
            {
                //sw.Flush();

                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\LogToday.txt"))
                {
                    sw.WriteLine("-------iRet1:" + iRet.ToString() + "----------");
                    sw.WriteLine(_PriShortChamberRecipeArray[0]);
                    sw.WriteLine(_PriShortChamberRecipeArray[25]);
                    sw.WriteLine(_PriShortChamberRecipeArray[50]);
                    sw.WriteLine("-------------------------------");

                    Thread.Sleep(0);
                    //这里就是要执行的任务,本处只显示一下传入的参数
                    iRet = _AxActQJ71E71TCP.WriteDeviceBlock2("R0", _PriShortChamberRecipeArray.Length, ref _PriShortChamberRecipeArray[0]);


                    sw.WriteLine("-------iRet2:" + iRet.ToString() + "----------");
                    sw.WriteLine(_PriShortChamberRecipeArray[0]);
                    sw.WriteLine(_PriShortChamberRecipeArray[25]);
                    sw.WriteLine(_PriShortChamberRecipeArray[50]);
                    sw.WriteLine("-------------------------------");

                }
            }
        }



如果开启一个线程,在线程的ThreadProc里面发送,发送到硬件里面的数据就是一组随机数据,不解原因。我在线程发送数据前与发送数据后写了log,数据也没问题,就是到硬件里面就变成随机数了

这个问题特别费解,我知道ref参数会在函数内部改变数据,但是为什么另外开启一个线程,发送的数据到硬件里面就不对了,但是写log是没问题的.
用backgroundworker发送也是一样,变成随机数了

请各位大侠给个解决办法吧。我的软件是用backgroundworker调用这个函数发送数据的

------解决方案--------------------
探讨
Application.OpenForms[0].Invoke //这个是winform的,可以用Invoke。
Application.Current.Windows[0] //W