void worker_DoWork(object sender, DoWorkEventArgs e) { try { var listMain = (from a in grdPostCodes.Rows.Where(c => c.Cells[COLS.Check].Value.ToBool() == true) select new { Id = a.Cells[COLS.Id].Value, PostCode = a.Cells[COLS.PostCode].Value.ToStr() }).ToList(); bool allowUpdating = false; foreach (var item in listMain) { using (TaxiDataContext db = new TaxiDataContext()) { allowUpdating = false; var listofCoordinates = db.stp_GetCoordinatesByRoadLevelData(item.Id.ToInt(), string.Empty).ToList(); if (item.PostCode.Length == 1 && listofCoordinates.Count != db.Gen_Coordinates.Count(c => c.PostCode[0] == item.PostCode[0] && SqlMethods.Like(c.PostCode, "[A-Z][0-9]%"))) { allowUpdating = true; } else if (item.PostCode.Length > 1 && listofCoordinates.Count != db.Gen_Coordinates.Count(c => c.PostCode.StartsWith(item.PostCode))) { allowUpdating = true; } if (allowUpdating) { CurrentRow cr = new CurrentRow(); cr.Total = listofCoordinates.Count; cr.UpdateValue = item.PostCode; int cnter = 0; foreach (var item2 in listofCoordinates) { try { if (IsFormClosed) { break; } db.Gen_Coordinates.InsertOnSubmit(new Gen_Coordinate { PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude }); db.SubmitChanges(); } catch { } cnter++; cr.index = cnter; worker.ReportProgress(cnter, cr); } // List<Gen_Coordinate> cc = new List<Gen_Coordinate>(); //foreach (var item2 in listofCoordinates) //{ // try // { // if (IsFormClosed) // break; // // db.Gen_Coordinates.InsertOnSubmit(new Gen_Coordinate { PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude }); // // db.SubmitChanges(); // if (cc.Count < 500) // { // cc.Add(new Gen_Coordinate { PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude }); // } // else // { // db.Gen_Coordinates.InsertAllOnSubmit(cc); // //db.Gen_Coordinates.InsertOnSubmit(new Gen_Coordinate { PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude }); // db.SubmitChanges(); // if (listofCoordinates.Count + 500 > listofCoordinates.Count) // { // cnter = listofCoordinates.Count; // } // else // { // cnter += 500; // } // cc.Clear(); // cr.index = cnter; // worker.ReportProgress(cnter, cr); // } // } // catch // { // } //} } } if (IsFormClosed) { break; } } e.Result = "Completed"; } catch (Exception ex) { e.Result = ex.Message; } }