Strange behavior with async

1
0

Hi.

i am using async / await in a form with a datagridview.

The dataGridView1_CellDoubleClick load the GUI correctly,

Wisej.Web.Application.Update(this, async () =>
{
this.ShowLoader = true;
await findCL(txtCL.Text);
this.ShowLoader = false;
});

 

the same does not happen with the tool linked to a textbox:

private async void txtCL_ToolClick(object sender, ToolClickEventArgs e)
{
if (e.Tool.Name==”TROVA”)
{
Wisej.Web.Application.Update(this, async () =>
{
this.ShowLoader = true;
await findCL(txtCL.Text);
this.ShowLoader = false;
});
}
}

in this second case the Loader remains visible until (for example) I click on the form bar in order to refresh the display.

The findCL method contains another “await” lo load data from Database.

I noticed that by commenting on that part, the interface seems to update correctly.
Is there any reason why it works in one event and not in the other?

 

  • You must to post comments
0
0

Thank you.

Now it’s ok, but a similar situation with textbox textchanged event

private async void textBoxNumero_TextChanged_1(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxNumero.Text)) { resetUI(false); }
else
{
string s = textBoxNumero.Text;
this.ShowLoader = true;
await findOrd(s, getRicType());
this.ShowLoader = false;
Wisej.Web.Application.Update(this); // update the client
}

}

seems not to update the UI. It is updated only if I move the form or click on it.
What is the best way to do a database search while writing text, without using autocomplete?

  • Luca (ITG)
    TextChanged is fired only when the value has changed on the server (when the textbox loses the focus) unless you attach a KeyDown event causing the roundtrip on every keystroke.
  • You must to post comments
2
0

When using async/await, the C# compiler adds a return statement and “extracts” all the following code into a new callback function. So when you have an “await” it’s like having a “return” statement.

Which means that the request coming from the browser receives a response, and the rest of the code will run “out-of-bounds”, basically without a corresponding request, so it’s unable to update the client unless you call Application.Update() to push the update (works only with WebSocket), or use the automatic polling, or something is requested by the browser.

In your case:

  • No need to call Application.Update(this, async…) because the await inside will cause the exit of the Update() block pushing an unnecessary update to the browser since you are already responding to a request.
  • The cellDoubleClick most likely causes a new request from the datagrid in the browser (begin edit or similar) which is then able to receive the update from the server.
  • The code is better like this:
private async void button1_Click(...) {
      
     this.ShowLoader = true;

     await Task.Delay(3000); // simulate async call
     // the client is update automatically since the request ends here.

     this.ShowLoader = false;
     Application.Update(this); // update the client
}
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.