在GUI线程数据库访问,坏,不是吗?线程、数据库、GUI

2023-09-05 00:32:16 作者:我的温柔喂过狗

我通过一些例子MSDN工作,并在ADO.Net一些书籍。 他们的共同点是使用点/点击/拖放设计时的功能在Visual Studio开发数据库应用程序,数据集绑定的控件等。

I'm working through some MSDN examples, and some books on ADO.Net. What they all have in common is using the point/click/drag-drop design time feature in Visual Studio to develop database applications, binding data sets to controls etc.

和产生的code确实在GUI线程中的所有数据库的访问,据我可以告诉。 这听起来像是不好的做法,并一度查询数据库访问应用程序的令人讨厌的行为,开始花时间。

And the resulting code does all DB access in the GUI thread, as far as I can tell. This sounds like bad practice, and annoying behavior of the application of the database access once queries starts to take time.

我缺少的东西?这是我们应该如何开发数据库应用程序? 我通常woudn't三思而后把网络IO在一个单独的线程,始终。

Am I missing something ? Is this how we're supposed to develop database applications ? I usually woudn't think twice about placing network IO in a seperate thread, always.

推荐答案

是的,这是一个坏主意,除非你真的不在乎具有GUI挂起。在某些情况下,特别是对快速和肮脏的工具,这实际上是正确的选择。如果这意味着你可以得到的东西谁只是打算用它几天反正和关心让做的很快,而不是总是有一个负责任的UI作业的用户,那么几乎没有一点浪费时间线程之间跳跃。

Yes, it's a bad idea unless you really don't care about having the GUI hang. In some cases, particularly for "quick and dirty" tools, that's actually the right choice. If it means you can get something to a user who's just going to use it for a couple of days anyway and cares about getting the job done soon rather than always having a responsive UI, then there's little point wasting time hopping between threads.

但是,没有,这不是你如何打算开发数据库应用程序这是为了保持响应。

But no, it's not how you're meant to develop database applications which are meant to stay responsive.

不过,我可以理解为什么书籍和教程做到这一点 - 至少在一定程度上。如果他们试图教你的数据库访问,而不是线程,这意味着的code更大比例将是相关的题材比,如果一切都绝对是生产code。我从经验中知道,这是很难保持教学code作为清洁一心只想。

However, I can understand why books and tutorials do it - at least to some extent. If they're trying to teach you database access rather than threading, it means that a greater proportion of the code is going to be relevant to the subject matter than if everything was absolutely "production code". I know from experience that it's tough to keep "teaching code" as clean as one would like.

不过,我认为它的将会的是这样的教程和书籍来解释这一权利前面是一个好主意,所以他们不会导致不良的生活习惯。

However, I think it would be a good idea for such tutorials and books to explain this right up front, so they don't lead to bad habits.