两线程之间进入w循环时不按循环里面的代码执行完毕,跳转到另一个w循环,不断的跳转都是如此,导致得到的数据有问题。初涉线程,遇到个这问题,求大神解答,谢谢
public void Run() { Thread t2 = new Thread(new ThreadStart(NumSearch_Run)); t2.IsBackground = true; t2.Start(); Thread t1 = new Thread(new ThreadStart(_12HourAgo_Run)); t1.IsBackground = true; t1.Start(); } private void NumSearch_Run() { int TimeNum = 1; // Config.Agent.PZAgent pzAgent = new PZAgent(PZBM); if (!pzAgent.Active) { this._Active = false; this._Active_error = pzAgent.Active_error; return; } while (true) { //获取定位点,对应表名称,读取的标识 Config.DBResult db = Config.DB.PZ.DB_PZ_Read(PZBM, "1"); if (!db.Active) { this._Active = false; this._Active_error = db.Error; return; } int Locate = Convert.ToInt32(db.Read_Result("MonistorLocate")); string JK_Link = db.Read_Result("JK_Link"); string LocateColumn = db.Read_Result("LocateColumn"); string ReadColumn = db.Read_Result("ReadColumn"); switch (Count) { case 1: TimeNum = TimeNum + 200; break; case 2: TimeNum = TimeNum + 400; break; case 3: TimeNum = TimeNum + 600; break; case 4: TimeNum = TimeNum + 800; break; case 5: TimeNum = TimeNum + 1000; break; } //判断上次和这次是否相等,相等就再加一倍 if (MonistorLocate == Locate) { _Count++; } else { _Count = 0; } _MonistorLocate = Locate; Hashtable hr = null; for (int i = 0; i < TimeNum; i++) { hr = new Hashtable(); //获取对应标识 db = Config.DB.AIM.DB_AIM_Locate_Read(ReadColumn, JK_Link, Locate.ToString(), "ID"); if (!db.Active) { this._Active = false; this._Active_error = db.Error; return; } // hr["ClinicID"] = db.Read_Result(ReadColumn); if (hr["ClinicID"].ToString() != "") { pzAgent.Run(hr, "1");//执行 if (!pzAgent.Active) { _Active = false; this._Active_error = pzAgent.Active_error; return; } //修改定位标识 db = Config.DB.PZ.DB_PZ_Locate_Upda(PZBM, Locate.ToString()); if (!db.Active) { this._Active = false; this._Active_error = db.Error; return; } TimeNum = 1; } Locate++; } Thread.Sleep(1000); } } private void _12HourAgo_Run() { Config.Agent.PZAgent pzAgent = new PZAgent(PZBM); if (!pzAgent.Active) { this._Active = false; this._Active_error = pzAgent.Active_error; return; } while (true) { //获取定位点,对应表名称,读取的标识 Config.DBResult db = Config.DB.PZ.DB_PZ_Read(PZBM, "1"); if (!db.Active) { this._Active = false; this._Active_error = db.Error; return; } string JK_Link = db.Read_Result("JK_Link"); string ReadColumn = db.Read_Result("ReadColumn"); string LocateColumn = db.Read_Result("LocateColumn"); Hashtable hr = null; // db = Config.DB.AIM.DB_AIM_Locate_List(ReadColumn, JK_Link, LocateColumn); if (!db.Active) { this._Active = false; this._Active_error = db.Error; return; } for (int i = 0; i < db._dt_Result.Rows.Count; i++) { hr = new Hashtable(); // hr["ClinicID"] = db._dt_Result.Rows[i][ReadColumn]; if (hr["ClinicID"] != "") { pzAgent.Run(hr, "1");//执行 if (!pzAgent.Active) { _Active = false; this._Active_error = pzAgent.Active_error; return; } } } Thread.Sleep(1000); } }
线程并不是你说的那样的跳来跳去,是因为你打了断点的原因,而你的线程没有做同步。
谢谢了,我要的就是两个异步执行.不过现在已经解决了。
多线程调试起来非常麻烦的,你不要因为断点跳来跳去就觉得他是这样执行的,有可能你看到的是当前线程的切换
实在没看清楚你的问题
简单的说,就是我想要当执行到while循环时是在内部执行完之后在执行另外个函数的while
@阿 牛: 你2个都是while(true)怎么会执行完?
@吴瑞祥: 谢谢了,while(true)当然是执行不完的,但是至少可以让他们异步执行。