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.