Should I use async "all the way" for my GUI?

I am an essential way to develop a significant WPF application that will manage complex hardware through serial communications. At the very bottom of the stack application, I have a serial number (using the .Net class "SerialPort"). Reading and writing to the h / w device is a fundamental part of the system used by many different areas of the business logic level, often interspersed with complex scientific calculations and other similar logic; these BLL methods, in turn, are called from all over the UI.

At present, everything in the business logic level, including reading and writing SerialPort, is synchronous. The only time I use async / wait is the UI level, usually when invoked in business logic. Here I will Task.Run() call in Task.Run() , for example: -

 private async void SomeButtonClick(object sender, EventArgs e) { await Task.Run(() => _someBLLService.TurnPumpOn()); } 

This is a simple example that takes milliseconds. A more complex example is to start a lengthy process (again using the above method); this "logic" of business logic will work for several seconds or minutes, collecting data from the hardware and performing other "management functions" during this time. Periodically, it will transfer data back to the user interface for plotting on a chart (through the pubsub event aggregator structure). The above method works well, allowing the user interface to remain responsive when business-level processes occur.

I recently looked at the possibility of "asynchronizing completely", starting with serial communication code (for example, using .ReadAsync ), but this can have consequences for the entire system. Dozens (if not hundreds) of business logic methods must be reorganized into asynchronous methods, right down to the user interface level. Here I could simplify the code above a bit:

 private async void SomeButtonClick(object sender, EventArgs e) { await _someBLLService.TurnPumpOn(); } 

As long as I don't mind doing big refactoring, I wonder what the benefits really are. Many people (like Steven Cleary) advocate for the “async all the way” solution, but the main goal is to keep the user interface responsive when invoking the long-running BLL process that my Task.Run() method already executes.

+5
source share
1 answer

Since your application is (presumably) not under heavy load and is a graphical interface, the only reason to use async and wait is to improve performance. Bandwidth and scalability are not included in the image.

Therefore, do what is most convenient for you. This could very well be the await Task.Run template that you used in the sample code.

but it will have consequences in the system

Yes. Async has notable performance, and it is contagious. Only do this if you can articulate the benefits that you get from this choice. The only advantage here would be that your user interface code is slightly shorter. This is the difference between your two pieces of code. The rest of the system is getting worse.

You may have been following the C # community and raising the trend to fully switch to asynchronous mode. Programmers are very prone to jumping into magical solutions without understanding them. Many proponents of asynchrony cannot even articulate the specific benefits they receive from it. There are many groups that also think. Do not base your decision on online consultations only. A factor that is in and check out for yourself that makes sense.

+7
source

All Articles