Async/await best practices in WiseJ

Answered
0
1

Hello all,

I’ve got a question regarding the best practices to manage async operations in WiseJ while also giving feedback to the user. I’ve gone throughout both the documentation, examples and forum questions related to this topic and was unable to find a concise answer that also worked as I think it should.

For example, when performing a long-running database operation that uses Entity Framework’s async methods, or calling a Web API that performs a task that doesn’t complete very quickly I tried to use the following pattern:

private async void DoSomething(object sender, EventArgs e)
{
Application.ShowLoader = true;
var result = await _API_Access.PerformAPICall();
// Use the result’s data
Application.ShowLoader = false;
Application.Update(this);
}

However awaiting the async’s method completion causes the application to return immediately and thus the loader also disappears, this is confusing to the end users and it also doesn’t depend on the type of async operation I perform, simply using await Task.Delay(5000); to fake an async call will cause the same behavior.
I’ve looked into both the Background Tasks example and the Stock Viewer example, Background Tasks only presents sync code and the Application.StartTask(() => {  }); calls are not awaited either, as for the Stock Viewer example it uses the Task.Run(() => {  }); pattern to run the async code in a separate thread.
So, which is the correct way to execute async operations in WiseJ? And how may I show feedback to the user about it correctly?

Best,
Ramses

  • You must to post comments
Best Answer
0
1

Hi Ramses,

I have attached a sample to showcase how you would exactly run async updates and push them to a client.

The sample doesn’t run a background task, but you can simply wrap everything and run it within the Application.StartTask() method.

Generally speaking, it’s better to use the component “ShowLoader” property instead of Application.ShowLoader, as it would be clearly visible what Window/Page/Control is waiting for the results.

Both Application.StartTask(() => {  }) and Task.Run(() => {  }) are correct, you can execute async functions however you want.

Let me know if this works well for you!

HTH,
Alaa

Attachment
  • You must to post comments
0
1

Also, just wanted to point out something!

The issue you had in the first place was actually a bug, when using Application.ShowLoader, the loader would be instantly hidden as soon as there is any incoming AJAX traffic.

But, it’s best to use the control’s ShowLoader or any main container control (Page,Form) for that matter.

The fix for this issue is currently in QA, and will be included in the next Wisej.NET release!

Best,
Alaa

  • Ramses Eitan
    Thank you so much! Both this and your previous reply helped clarify things
  • Frank (ITG)
    Hi Ramses, the fix Alaa mentioned has been deployed with Wisej.NET 3.2.4 Best regards, Frank
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.