1

我在我的应用程序中创建了两个线程,第一个是主 UI 线程,它将完成正常的工作,例如下面的查询

      var order = db.Orders.Where(x=>x.systemId==id).ToList();

另一个线程正在检查是否有任何警告情况需要处理,代码如下:

      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();
            }

在这种情况下,主 UI 线程中的查询会很慢,有时甚至无法正常工作。

任何人都可以帮助我吗?

4

1 回答 1

0

如果我错了,请纠正我,但您似乎在循环中向 Orders 插入值,并且还尝试在主 UI 中使用表达式。

此外,5 秒的间隔可能很短,因为您的 LINQ 表达式变得更加复杂,因此计算所需的时间会增加。

随着您的数据库获取新记录,加载“BathDBDataContext”可能需要更多时间。我建议使用缓存并增加 5 秒刷新计时器。

于 2013-05-15T14:31:26.397 回答