private void readCalcResultAsync(IntPtr Chandle, int dataSize) { //-1代表子进程写入共享内存失败 if (dataSize == -1) { Console.WriteLine(string.Format("Write mmf {0} failed", Chandle)); System.Environment.Exit(2); } //0代表当前共享内存中不存在数据,则直接返回,且让++this.procDoneNum if (dataSize == 0) { Console.WriteLine(string.Format("当前批{0}没有数据", Chandle)); if (++this.procDoneNum == this.processNum) { Task.Run(() => { writeResToDb(); }); } return; } DateTime t1, t2; t1 = DateTime.Now; Console.WriteLine(string.Format("read params :child handle = {0}, data size = {1}", Chandle, dataSize)); string shareName = this.getMMFName(Chandle); Console.WriteLine(string.Format("read child outcome :{0}", shareName)); IntPtr mmf = IntPtr.Zero; try { mmf = MMF.OpenFileMapping(MMF.FileMapAccess.FileMapRead, false, shareName); Console.WriteLine("open file ..."); } catch (Exception e) { //MessageBox.Show(string.Format("open mmf {0} failed, error : {1}", shareName, e.Message)); Console.WriteLine(string.Format("open mmf {0} failed, error : {1}", shareName, e.Message)); System.Environment.Exit(2); } if (IntPtr.Zero == mmf) { //MessageBox.Show(string.Format("共享内存<{0}>打开失败,错误信息编号:{1}", shareName, MMF.GetLastError())); Console.WriteLine(string.Format("共享内存<{0}>打开失败,错误信息编号:{1}", shareName, MMF.GetLastError())); System.Environment.Exit(2); return; } IntPtr reader = IntPtr.Zero; try { reader = MMF.MapViewOfFile(mmf, MMF.FileMapAccess.FileMapRead, 0, 0, (uint)dataSize); Console.WriteLine("map view ..."); } catch (Exception e) { Console.WriteLine("map view failed"); System.Environment.Exit(2); return; } if (reader == IntPtr.Zero) { //MessageBox.Show(string.Format("共享内存<{0}>映射失败,错误信息编号:{1}", shareName, MMF.GetLastError())); Console.WriteLine(string.Format("共享内存<{0}>映射失败,错误信息编号:{1}", shareName, MMF.GetLastError())); System.Environment.Exit(2); return; } IntPtr tp = IntPtr.Zero; Type type = typeof(MMFGSStruct); int ssize = Marshal.SizeOf(type); Console.WriteLine("ssize = " + ssize); //tp = new IntPtr(reader.ToInt64()); MMFGSStruct data = new MMFGSStruct(); long sp = reader.ToInt64(); for (int dsize = 0; dsize < dataSize; dsize += ssize) { try { tp = new IntPtr(sp + dsize); data = (MMFGSStruct)Marshal.PtrToStructure(tp, typeof(MMFGSStruct)); // convertFromMMFGSStruct 将共享内存传递的计算结果转换为GridStrength this.MultiTasksGridStrengths.Add(convertFromMMFGSStruct(data)); } catch (Exception e) { Console.WriteLine(string.Format("read error : tp = {0}, dsize = {1}, msg = {2}", tp.ToInt64(), dsize, e.Message)); System.Environment.Exit(2); //MessageBox.Show(string.Format("read error : tp = {0}, dsize = {1}, msg = {2}", tp.ToInt32(), dsize, e.Message)); } } Console.WriteLine("after read...."); MMF.UnmapViewOfFile(reader); MMF.CloseHandle(mmf); mmf = reader = IntPtr.Zero; t2 = DateTime.Now; Console.WriteLine(string.Format("read {0} done. now total : {1}, using time: {2}", shareName, this.MultiTasksGridStrengths.Count, (t2 - t1).TotalMilliseconds)); Console.WriteLine("**********************************************************************"); this.procDoneNum++; Console.WriteLine(String.Format("已处理{0}/{1}个进程", this.procDoneNum, this.processNum)); Console.WriteLine("**********************************************************************"); //this.outGSs(); //if (++this.procDoneNum == Math.Min(this.maxProcNum, this.processNum - this.procDoneNum1)) if (this.procDoneNum == this.processNum) { Task.Run(() => { writeResToDb(); }); RedisMq.Pub("rayTrace_finish", this.cellInfo.eNodeB); } }
// 2019.5.22 private void readReRayResult(IntPtr Chandle, int dataSize) { //-1代表子进程写入共享内存失败 if (dataSize == -1) { Console.WriteLine(string.Format("Write mmf {0} failed", Chandle)); errorExit(); } if (dataSize == 0) { Console.WriteLine(string.Format("当前批{0}没有数据", Chandle)); //将写入数据库操作剥离 if (++this.procDoneNum1 == this.processNum) { writeReRayToDb(); } return; } string shareName = this.getMMFName(Chandle) + "_ReRay"; IntPtr mmf = IntPtr.Zero; try { mmf = MMF.OpenFileMapping(MMF.FileMapAccess.FileMapRead, false, shareName); } catch (Exception e) { Console.WriteLine(string.Format("open mmf {0} failed, error : {1}", shareName, e.Message)); errorExit(); } if (IntPtr.Zero == mmf) { Console.WriteLine(string.Format("共享内存<{0}>打开失败,错误信息编号:{1}", shareName, MMF.GetLastError())); errorExit(); } //共享内存打开成功通知 Console.WriteLine(string.Format("read reRay................. open mmf {0} success", shareName)); IntPtr reader = IntPtr.Zero; try { reader = MMF.MapViewOfFile(mmf, MMF.FileMapAccess.FileMapRead, 0, 0, (uint)dataSize); } catch (Exception e) { Console.WriteLine(e.Message); errorExit(); } if (reader == IntPtr.Zero) { Console.WriteLine(string.Format("共享内存<{0}>映射失败,错误信息编号:{1}", shareName, MMF.GetLastError())); errorExit(); } IntPtr tp = IntPtr.Zero; Type type = typeof(MMFReRayStruct); int ssize = Marshal.SizeOf(type); tp = new IntPtr(reader.ToInt64()); MMFReRayStruct data = new MMFReRayStruct(); long sp = reader.ToInt64(); for (int dsize = 0; dsize < dataSize; dsize += ssize) { try { tp = new IntPtr(sp + dsize); data = (MMFReRayStruct)Marshal.PtrToStructure(tp, typeof(MMFReRayStruct)); this.MultiTasksReRay.Add(data); } catch (Exception e) { Console.WriteLine(string.Format("read error : tp = {0}, dsize = {1}, msg = {2}", tp.ToInt64(), dsize, e.Message)); } } MMF.UnmapViewOfFile(reader); MMF.CloseHandle(mmf); mmf = reader = IntPtr.Zero; //共享内存打开成功通知 Console.WriteLine(string.Format("add reRay to MultiTasksReRay success...................................")); //将写入数据库操作剥离 if (++this.procDoneNum1 == this.processNum) { writeReRayToDb(); } }