Good morning everyone. I'll tell you about the situation, I have a button that when clicked shows the message in a label and I have a method that should show another message after 3 seconds, but it doesn't work for me. I tried it in console and the method is executed. I don't understand the reason.
<form runat="server">
<asp:Label runat="server" ID="lblMessage"></asp:Label>
<asp:Button runat="server" OnClick="btn_Click" ID="btn" Text="Probar" />
</form>
protected void btn_Click(object sender, EventArgs e){
lblMessage.Text = "hola";
Task.Delay(new TimeSpan(0, 0, 3)).ContinueWith(o => { lblMessage.Text = "3 segundos"; });
}
When you call
Task.Delay(...
the code it is creating a new thread for that action, but btn_click terminates and the rendering of the page continues. it is part of the ASP.NET Life Cycle .In your case, you should use JavaScript for the delay.