private void GenerateVouchers() { decimal Total = VSDetailList.Sum(x => x.Quantity); string Status = "{0} of " + Total.ToString() + " Done"; Dispatcher d = Dispatcher.CurrentDispatcher; ThreadPool.QueueUserWorkItem(x => { using (SqlConnection conn = new SqlConnection(GlobalClass.TConnectionString)) { conn.Open(); using (SqlTransaction tran = conn.BeginTransaction()) { foreach (TParkingSalesDetails pSD in _VSDetailList) { VoucherType vt = VTypeList.First(y => y.VoucherId == pSD.ProdId && y.SkipVoucherGeneration == false); for (int i = 1; i <= pSD.Quantity; i++) { Voucher v = new Voucher() { BillNo = pSD.BillNo, ExpDate = CurDate.AddDays(vt.Validity), ValidStart = vt.ValidStart, ValidEnd = vt.ValidEnd, VoucherName = vt.VoucherName, Value = vt.Value, Sno = i, VoucherId = vt.VoucherId, FYID = GlobalClass.FYID }; do { v.Barcode = "#" + new Random().Next(1677215).ToString("X"); }while (tran.Connection.ExecuteScalar <int>("SELECT COUNT(*) FROM ParkingVouchers WHERE Barcode = @Barcode", v, transaction: tran) > 0); v.Save(tran); ParkingVouchers.Add(v); d.BeginInvoke((Action)(() => { GenCount = string.Format(Status, ParkingVouchers.Count); Progress = ParkingVouchers.Count / Total * 100; })); } } tran.Commit(); PrintVouchers(_VSDetailList.First().BillNo, true, d); } } }); }
private async Task GenerateVouchers(SqlTransaction tran) { decimal Total = VSDetailList.Sum(x => x.Quantity); string Status = "{0} of " + Total.ToString() + " Done"; await Task.Run(() => { foreach (TParkingSalesDetails pSD in _VSDetailList) { VoucherType vt = VTypeList.FirstOrDefault(y => y.VoucherId == pSD.ProdId && y.SkipVoucherGeneration == false); if (vt != null) { for (int i = 1; i <= pSD.Quantity; i++) { Voucher v = new Voucher() { BillNo = pSD.BillNo, ExpDate = CurDate.AddDays(vt.Validity), ValidStart = vt.ValidStart, ValidEnd = vt.ValidEnd, VoucherName = vt.VoucherName, Value = vt.Value, Sno = i, VoucherId = vt.VoucherId, FYID = GlobalClass.FYID }; do { v.Barcode = "#" + new Random().Next(1677215).ToString("X"); }while (tran.Connection.ExecuteScalar <int>("SELECT COUNT(*) FROM ParkingVouchers WHERE Barcode = @Barcode", v, transaction: tran) > 0); v.Save(tran); ParkingVouchers.Add(v); GenCount = string.Format(Status, ParkingVouchers.Count); Progress = ParkingVouchers.Count / Total * 100; } } } }); }