简体   繁体   English

保持UI线程空闲

[英]Keep UI Thread free

I have a form with two panels(top, bottom), each panel contains grids. 我有一个带有两个面板(顶部,底部)的表单,每个面板包含网格。 Basically, it's a Master-Detail form where selecting a row from the top grid would show details in the bottom grid. 基本上,这是一个Master-Detail表单,其中从顶部网格中选择一行将在底部网格中显示详细信息。

Binding the data to the detail grid is taking some time. 将数据绑定到详细信息网格需要花费一些时间。 Since binding is done on UI thread, it blocks the thread and therefore the user cannot select another row from the master grid until the binding is done. 由于绑定是在UI线程上完成的,因此它将阻塞线程,因此用户无法从主网格中选择另一行,直到绑定完成。

Please note that by binding I don't mean getting data from data source. 请注意,绑定并不意味着从数据源获取数据。 It's the actual binding that's taking longer as it does a lot of data massaging. 由于实际绑定需要花费大量时间进行大量数据按摩,因此它花费的时间更长。 How can I keep the UI thread free while the detail grid is doing it's binding? 在细节网格进行绑定时,如何保持UI线程空闲?

Thanks a million. 太感谢了。

You can't. 你不能 The update of the UI has to be performed on the UI thread. UI的更新必须在UI线程上执行。

You may be able to speed up the binding by using things such as BeginUpdate/EndUpdate which is available on some controls but as you don't specify what you are using I can't say if that's available. 您可能可以通过使用某些控件上可用的诸如BeginUpdate / EndUpdate之类的方法来加快绑定速度,但是由于您未指定正在使用的内容,因此无法确定是否可用。

您可以使用后台线程来执行数据检索,然后使用UI线程来显示它。

If I were you, it sounds like you are dealing with a LOT of data, and I would separate all the "massaging" you can into a separate thread process. 如果我是您,听起来您正在处理很多数据,那么我会将所有“按摩”都分离到一个单独的线程进程中。

So maybe for example when a master record is created, you "manually" spin off the detail data in a background thread to another dataset and do the massaging, then just bind the resulting dataset to the grid. 因此,例如,当创建主记录时,您可以“手动”将后台线程中的明细数据剥离到另一个数据集并进行按摩,然后将结果数据集绑定到网格。 That way the only thing taking place on the UI thread is just the UI binding. 这样,UI线程上发生的唯一事情就是UI绑定。

Ultimately if it's taking that long, you may be approaching a critical point in your application where you need to manually do what you need to do in code rather than using the out of the box data binding features in .NET. 最终,如果要花费这么长时间,您可能正在接近应用程序中的关键点,您需要手动执行代码中需要做的事情,而不是使用.NET中的现成数据绑定功能。

Finally I found the solution. 终于我找到了解决方案。 The solution doesn't include multithreading to start with. 该解决方案不包括多线程开始。 As I said that the delay was in binding the grid meaning the main thread was held, we couldn't do much. 正如我所说的,延迟是在绑定网格(这意味着保持主线程)时,我们不能做太多事情。 So the solution is to bring delays. 因此解决方案是带来延迟。 When user selects the master row, a timer ticks off for a certain time. 当用户选择主行时,计时器将计时一段时间。 If another request is made before the time is expired, timer gets restarted. 如果在时间到期之前再次发出请求,计时器将重新启动。 This is we ignore all calls that are made because user was clicking or selecting rows too fast. 这是因为用户单击或选择行的速度太快,所以我们忽略了所有调用。 Once timer is expired, I take the selected row and display data. 计时器到期后,我进入所选行并显示数据。 Simple and elegant solution. 简单而优雅的解决方案。

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

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