DEV Community

Cover image for Ability to Create Async Commands in WebForms Core 2
Elanat Framework
Elanat Framework

Posted on

Ability to Create Async Commands in WebForms Core 2

In version 2 of WebForms Core technology, we will be able to execute commands asynchronously. This is a new feature that is structurally similar to conditional commands in the Condition section of the WebForms class on the server.

WebForms Core Async Commands

Async execution in a section of commands allows the application to perform multiple tasks simultaneously without waiting for the completion of a time-consuming operation (such as a network request or the execution of a heavy method). This approach increases system performance, because the CPU is not idle while waiting for an input/output (I/O) response and can perform other tasks. As a result, the application's responsiveness increases, the user experience improves, and resource consumption is more optimized. In short, Async enables the simultaneous execution of independent tasks and increases the overall performance of the application.

Asynchronous operations are the cornerstone of modern web development.


Example Async in WebForms Core technology

To follow the Async structure in WebForms Core technology, simply call the "Async" method and then write the command you need to execute Async.

Async next command

form.Message("Message before Async"); // Previous command
form.Async();
form.AddText("<main>", Fetch.LoadUrl("/static/page.html"));
form.Message("Message after Async"); // Next command
Enter fullscreen mode Exit fullscreen mode

How it works?

  • In the above example, the message "Message before Async" is displayed first.
  • Then, due to the time-consuming (even a few milliseconds) operation of loading the page from the server, the message "Message after Async" is displayed.
  • And after the page load operation, the page content is added to the "main" tag.

If you want a set of commands to be executed Async, you need to do the following:

  1. Call the "Async" method
  2. Call the "StartBracket" method
  3. Create the set of commands
  4. Call the "EndBracket" method

Async multi command

form.Message("Message before Async"); // Previous command
form.Async();
form.StartBracket();
form.AddText("<main>", Fetch.LoadHtml("/template/content.html", "Article|<section>"));
form.Replace("<main>|<section>-1", "@ArticleTitle", "My article", false, true);
form.Replace("-", "@ArticleText", "This is the text of the article.", false, true);
form.EndBracket();
form.Message("Message after Async"); // Next command
Enter fullscreen mode Exit fullscreen mode

How it works?

Here too, the functionality is similar:

  • In the above example, first the message "Message before Async" is displayed.
  • Then, because the template loading process from the server takes time, the message "Message after Async" is displayed.
  • And after the template loading process, the page content is added in the "main" tag. And then the placeholders are replaced.

WebForms Core in GitHub: https://github.com/webforms-core

Top comments (0)