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

落爺英雄遲暮 提交于 2019-12-14 02:27:18

问题


I have an order manager application created in C# and WPF. The order manager application needs to communicate back and forth with a shipping application that is written in a completely different shipping language. The method of communication between the programs is an XML file whose EnableShipping attribute is either a 0 or a 1 and a SQL database.

In my order manager application I have button that "Begins shipping" and changes the EnableShipping attribute from a 0 to a 1. The shipping application is looping and reads this value, begins shipping all of the orders whose certain attribute matches a string, changes this attribute to a different string, and marks a different attribute (Status Changed) to 1.

In my order manager application, as of now I have a thread that continually loops and checks the database for orders with a Status Changed attribute of 1, makes the changes to the UI and writes back to the database, Status Changed = 0.

Here is some code to show what my order manager application is doing in the thread.

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

So Even if you don't know exactly what is going on in the code, you know that I have to continuously check the database for status changed items, do work on those items in my collection as well as in the database, update the UI, and keep repeating the process.

At first I thought a good old thread would work like my code is doing now, but the UI becomes unresponsive when I am "doing work". I looked at some background worker code online seeing if maybe that would be a good choice for better UI responsiveness, but didn't know if this is a good solution as I need to continuously keep doing work and updating the UI.

Any thoughts or suggestions? appreciate it...


回答1:


You could use BackGroundWorker with ReportsProgess to send the info to UI thread. You can pass an object. Also implement cancellation so you you don't shut down stream.

BackgroundWorker.ReportProgress Method

The other option is there is a way to get SQL notification.

Query Notifications in SQL Server



来源:https://stackoverflow.com/questions/12519746/what-type-types-of-threading-should-i-use-in-wpf-and-c-sharp-for-my-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!