c# linq to sql two threads visit the same db, but one is very slow -
i've created 2 threads in application , first 1 main ui thread, normal job quering below
var order = db.orders.where(x=>x.systemid==id).tolist();
the other thread checking if there warning situation handle, code below:
transactionoptions transoptions = new transactionoptions() { isolationlevel = system.transactions.isolationlevel.readuncommitted }; using (new transactionscope(transactionscopeoption.required, transoptions)) { bathdbdatacontext dc = new bathdbdatacontext(connectionstring); var all_menus = dc.menu.where(x => x.addautomatic); var menus = all_menus.select(x => x.name).tolist(); var orders = dc.orders.where(x => menus.contains(x.menu) && !x.paid && x.deleteemployee == null); var add_menus = orders.select(x => x.menu).distinct(); var ids = orders.select(x => x.systemid).distinct(); foreach (var systemid in ids) { var seat_orders = orders.where(x => x.systemid == systemid); foreach (var add_menu in add_menus) { var add_orders = seat_orders.where(x => x.menu == add_menu && (x.pricetype == null || x.pricetype != "stop counting")); if (add_orders.count() == 0) continue; var max_time = add_orders.max(x => x.inputtime); var max_order = add_orders.orderbydescending(x => x.inputtime).firstordefault(); //var max_order = add_orders.firstordefault(x => x.inputtime == max_time); if (max_order == null || max_order.pricetype == "per hour") continue; var the_menu = all_menus.firstordefault(x => x.name == add_menu); string menu_time = the_menu.timelimithour.tostring() + ":" + the_menu.timelimitminiute.tostring() + ":" + the_menu.timelimitsecond.tostring(); timespan tsm = timespan.parse(menu_time); if (datetime.now - max_order.inputtime < tsm) continue; if (the_menu.addtype == "by unit") { orders new_order = new orders(); new_order.menu = max_order.menu; new_order.text = max_order.text; new_order.systemid = systemid; new_order.number = 1; new_order.money = the_menu.price; new_order.technician = max_order.technician; new_order.techtype = max_order.techtype; new_order.inputtime = datetime.now; new_order.inputemployee = "computer"; new_order.paid = false; dc.orders.insertonsubmit(new_order); } else if (the_menu.addtype == "by time") { orders new_order = new orders(); new_order.menu = max_order.menu; new_order.text = max_order.text; new_order.systemid = systemid; new_order.number = 1; new_order.pricetype = "per hour"; new_order.money = convert.todouble(the_menu.addmoney); new_order.technician = max_order.technician; new_order.techtype = max_order.techtype; new_order.inputtime = datetime.now; new_order.inputemployee = "computer"; new_order.paid = false; dc.orders.insertonsubmit(new_order); } } dc.submitchanges(); }
in situation, query in main ui thread slow or doesn't work.
can me that?
correct me if wrong seem insert values orders in loop , trying use expression in main ui.
besides 5 seconds interval might short since linq expressions more complex time needed calculations increases.
loading "bathdbdatacontext" might take more time database gets new records. i'd suggest using cache , increase 5 seconds refresh timer.
Comments
Post a Comment