日期:2014-05-18 浏览次数:20880 次
/// <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]); }
/// <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("-------------------------------"); } } }