Accessing a database in a GUI thread is bad, isn't it?

I am working on some MSDN examples and some ADO.Net books. All they have in common uses the point / click / drag design time function in Visual Studio to develop database applications, bind datasets to controls, etc.

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

Am I missing something? Is this how we should develop database applications? I usually don’t think twice about placing network I / O in a separate thread, always.

+4
source share
6 answers

Yes, that’s a bad idea if you really don’t care about the GUI being dependent. In some cases, especially for “quick and dirty” tools, this is really the right choice. If this means that you can get something for a user who will still use it for a couple of days and takes care to do the work soon, and not always have a responsive interface, then it makes little sense to spend time switching between threads.

But no, that’s not how you intend to develop database applications that should remain responsive.

However, I can understand why books and textbooks do this - at least to some extent. If they try to teach you access to a database rather than streaming, it means that most of the code will be relevant to the subject than if everything were absolutely “production code”. From experience, I know that it is difficult to keep the "training code" as clean as I would like.

However, I think it would be nice if such textbooks and books explained this directly in front of you, so they do not lead to bad habits.

+5
source

Note that the main problem with this practice has nothing to do with threading, but with sharing problems. By connecting to database access as much as the user interface, they lose the capabilities available for sharing and will need to change the database when changing the user interface and vice versa.

It doesn't matter if it is important to be in the same stream or not, it will depend on the circumstances, the platform, etc.

+2
source

Many MSDN examples and Visual Studio itself encourage the “smart form” antipattern, where you drag stuff onto a canvas and attach a little code, and you have something working. They are aimed at an easy entry level without much difficulty with this material.

It would be best to have a workflow to do something intensive and intense response to your GUI (unless the goal is to make something work after 10 minutes).

+1
source

Yes its bad .

What if your request takes 20 seconds? Goodbye.

IMHO, the reason why so many samples are built is because the asynchronous templates in .Net are pretty messy.

Imagine your samples looked like this:

private void BasicAsyncWithNoProgress() { if (Application.Current.Dispatcher.Thread != System.Threading.Thread.CurrentThread) { Application.Current.Dispatcher.Invoke((System.Windows.Forms.MethodInvoker) BasicAsyncWithNoProgress, null); return; } // Do Work } 

Compare this to invented syntax (which can be achieved with postharp)

 [NotOnUIThread] private void BasicAsyncWithNoProgress() { // Do Work } 
+1
source

In many cases, you will definitely want to implement a separation between the GUI and the data access code.

However, most books provide information about features, not the best examples of architecture.

0
source

Whether this is bad or not entirely dependent on the type of application. If the requests are of such a nature that they do not take a lot of time, it might be a good idea to preserve the complexity of stream processing. But as soon as you have time-consuming requests, you probably want to have them on your workflow.

0
source

Source: https://habr.com/ru/post/1286648/


All Articles