繁体   English   中英

在我的场景中,我应该在WPF和c#中使用哪种类型的线程……常规线程,后台工作程序或线程池?

[英]What type/types of threading should I use in WPF and c# for my scenario… regular threads, background worker, or thread pool?

我有一个用C#和WPF创建的订单管理器应用程序。 订单管理器应用程序需要与以完全不同的运输语言编写的运输应用程序来回通信。 程序之间的通信方法是XML文件(其EnableShipping属性为0或1)和SQL数据库。

在我的订单管理器应用程序中,我有一个“开始运送”按钮,并将EnableShipping属性从0更改为1。运送应用程序正在循环并读取此值,开始运送所有某些属性与字符串匹配的订单,将其更改属性指定为其他字符串,并将其他属性(状态更改)标记为1。

在我的订单管理器应用程序中,截至目前,我有一个线程不断循环并检查数据库中状态更改为1的订单,对UI进行更改并写回状态更改为0的数据库。

这是一些代码,显示我的订单管理器应用程序在线程中正在做什么。

while(true)
        {
            string enabled = null;
            currentInstance = OrderManager.getCurrentInstance();
            SqlCommand command = new SqlCommand("Select OrderNumber, BatchStatus from dbo.Orders where StatusChanged='1'", connection1);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Int64 orderNum = (Int64)reader[0];
                    int index = linqForOrderIndex(orderNum);
                    string batchStatus = (string)reader["BatchStatus"];
                    SqlCommand statusChangedFalse = new SqlCommand("Update dbo.orders Set StatusChanged = '0' where OrderNumber = '" + orderNum + "'", connection2);
                    switch (batchStatus)
                    {
                        case "Untouched":
                            currentInstance.Orders[index].batchStatus = "Untouched";
                            break;
                        case "Batch Ready":
                            currentInstance.Orders[index].batchStatus = "Batch Ready";
                            break;
                        case "Shipped":
                            currentInstance.Orders[index].batchStatus = "Shipped";
                            break;
                        case "Error":
                            currentInstance.Orders[index].batchStatus = "Error";
                            break;
                    }
                    statusChangedFalse.ExecuteNonQuery();

                    Thread.Sleep(1000);

                    reader.Close();
                    reader = command.ExecuteReader();
                }

                currentInstance.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(
                        delegate()
                        {
                            currentInstance.refreshTreeFilters();
                            currentInstance.refreshOrderCounts();
                            currentInstance.batchReadyView();
                        }
                    )); 
            }
      }

因此,即使您不确切知道代码中发生了什么,您也知道我必须不断检查数据库中状态更改的项目,对集合中的项目以及数据库中的项目进行处理,更新UI ,并继续重复该过程。

最初,我认为一个好的旧线程可以像我的代码现在一样工作,但是当我“正在工作”时,UI变得无响应。 我在网上查看了一些后台工作人员代码,看这是否可能是更好的UI响应能力的一个不错的选择,但是我不知道这是否是一个好的解决方案,因为我需要不断地工作并更新UI。

有什么想法或建议吗? 感谢...

您可以将BackGroundWorker与ReportsProgess一起使用,以将信息发送到UI线程。 您可以传递一个对象。 还可以执行取消操作,以免关闭流。

BackgroundWorker.ReportProgress方法

另一个选择是有一种获取SQL通知的方法。

SQL Server中的查询通知

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM