ObjectListView elapsed time column Potentially needs Multi-Threading

回眸只為那壹抹淺笑 提交于 2019-12-11 21:04:53

问题


I should first mention I am new to programming. I will explain my problem and my question to the best of my ability.

I was wondering if it is possible to update a single column of an ObjectListView (from here on referred to as OLV). What I would like is to have a column that would display the "Elapsed Time" of each row. This Elapsed Time column would be refreshed every 15 to 30 seconds.

How I set my OLV

myOLV.SetObjects(GetMyList());

GetMyList method returns a list populated from a simple database query.

Within the GetMyList method, it would convert the DateTime column into a string showing the elapsed time. This is then shown by the OLV as a string, not a datetime...

elapsed_time = ((TimeSpan)(DateTime.Now - x.time_arrived)).ToString("hh\\:mm\\:ss");

How I was trying to make this work was by using a timer that would every 30 seconds recall the GetMyList method. Because the method would re-query the database and return the records it retrieved, they were more up to date. This worked fine until I had more than 20 rows in the OLV. Each "refresh" for 200 rows takes 4 seconds to complete. That is 4 seconds that the UI is unresponsive...

As I am new to programming, I have never done anything with multi-threading, but I did a little reading up on it and attempted to create a solution. Even when I create a new thread to "refresh" the OLV object, it still causes the entire form to become unresponsive.

My question is, how can I have a column within my ObjectListView refresh every 15-30 seconds without causing the entire UI to become unresponsive? Is it even possible/ a good idea to have the ObjectListView refresh in the background and then display it when it's ready?


回答1:


The problem is the run time of the database query, not the updating of ObjectListView (updating 200 rows in ObjectListView should take about 200ms). You need to get the database work off the UI thread and onto a background thread. That background thread can then periodically fetch the updated data from the database. Once you have the updated data, updating ObjectListView is very fast.

However, multi-threading is a deep-dive topic that is likely to bite you. There are many things that can go wrong. You would be better off having a button on your UI that the user can click to Refresh the grid, and running everything synchronously. Once the synchronous version is working perfectly, then start working on the much more error prone multi-threaded version.

To strictly answer your question, you would do something like this:

var thread = new Thread(_ => {
    while (!_cancelled) {
        Thread.Sleep(15 * 1000);
        if (!_cancelled) {
            var results = QueryDatabase();
            this.objectListView1.SetObjects(results);
        }
    }
});
thread.Start();

_cancelled is a boolean variable in your class that allows you to cancel the querying process. QueryDatabase() is your method that fetches the new data.

This example doesn't deal with errors, which are a significant issue from background threads.

Another gotcha is that most UI components cannot be updated from a background thread, however, ObjectListView has a few methods that are thread-safe and SetObjects() is one of them so you can call it like this.

Just to repeat: you really should start with the synchronous version, and only start thinking about async once you are happy with that one.



来源:https://stackoverflow.com/questions/26107258/objectlistview-elapsed-time-column-potentially-needs-multi-threading

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