public void RunQueryThread() { //开启一个线程,不停的获取数据,然后查询数据 System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(async() => { var db = new ApplicationDbContext(); var rep = new PhotoImageRepository(db); var service = new UFaceService(); while (true) { var b = false; lock (exitLock) { b = bExit; } if (b) { break; } if (queuePhoto.Count == 0) { System.Threading.Thread.Sleep(10); continue; } PhotoImageQueryItem item = null; lock (queueLock) { item = queuePhoto.Dequeue(); } if (item == null) { continue; } try { //查询数据 var list = await service.FaceFind(item.Camera, item.PersonID); if (list != null && list.Length > 0) { //找到对应的;然后更新 foreach (var v in list) { if (string.Compare(v.faceId, item.FaceID) == 0) { var photo = new PhotoImage() { ID = item.PhotoImageID, Feature = v.feature, FeatureKey = v.featureKey, }; rep.UpdateFeature(photo, null); break; } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp); } // System.Threading.Thread.Sleep(10); } })); thread.IsBackground = true; thread.Start(); }
public void RunQueryThread() { //开启一个线程,不停的获取数据,然后查询数据 System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(async() => { var db = new ApplicationDbContext(); var rep = new CheckinRecordRepository(db); var repCamera = new CameraRepository(db); var repEmployee = new EmployeeRepository(db); var service = new UFaceService(); DateTime prev = DateTime.Now; while (true) { if (HasExit) { break; } try { if (dictCamera == null || dictCamera.Length == 0) { //查询数据库获取相机信息 var cameras = repCamera.Get(); dictCamera = cameras.ToArray(); } if (dictCamera == null) { System.Threading.Thread.Sleep(10); continue; } foreach (var v in dictCamera) { if (HasExit) { break; } Camera camera = v; int length = -1; int index = 0; string startTime = "0"; string endTime = "0"; var now = DateTime.Now; if (dictDateTime.ContainsKey(v.IP)) { //如果在deltaTime毫秒之内,不做处理 var delta = now - dictDateTime[v.IP]; if (delta.Minutes * 60000 + delta.Seconds * 1000 + delta.Milliseconds < deltaTime) { continue; } // startTime = dictDateTime[v.IP].ToString(@"yyyy-MM-dd hh\:mm\:ss"); endTime = now.ToString(@"yyyy-MM-dd hh\:mm\:ss"); dictDateTime[v.IP] = now; } else { dictDateTime.Add(v.IP, now); } var ret = await service.findRecords(camera, "-1", length, index, startTime, endTime); if (null != ret) { //把数据插入数据库 if (null != ret.records) { //这里面的操作都比较费时 if (HasExit) { break; } var lst = new List <CheckinRecord>(); foreach (var rec in ret.records) { //需要从数据库获取数据 var peoples = repEmployee.Get(x => x.Code != null && x.Code.CompareTo(rec.personID) == 0); Employee employee = peoples == null || peoples.Count() == 0 ? null : peoples.ElementAt(0); var cr = new CheckinRecord() { InnerID = rec.ID, IP = camera.IP, Path = rec.path, PersonId = rec.personID, Time = rec.time, Type = rec.type, State = rec.state, CheckinTime = Utils.TimeHelper.UnixTime2DateTime(rec.time), //需要从unix时间戳转化得到 }; if (employee != null) { cr.EmployeeID = employee.ID; } // lst.Add(cr); } rep.Add(lst.ToArray()); } } // System.Threading.Thread.Sleep(10); } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp); } System.Threading.Thread.Sleep(10); } })); thread.IsBackground = true; thread.Start(); }