Alex E.
asked on
ASP.NET C# System.Threading.Thread.Sleep and text label problem
I have part of this code:
This code update a label text and wait 5 seconds before continue the rest of the code. Now the problem is that the wait for 5 seconds occur but the label1.text is not updated. How can we update that label1.text then wait for the 5 seconds then continue with the rest of the code? We have in the code around 35 text labels in similar way.
label1.text = "System Working Please Wait...";
System.Threading.Thread.Sleep(5000);
This code update a label text and wait 5 seconds before continue the rest of the code. Now the problem is that the wait for 5 seconds occur but the label1.text is not updated. How can we update that label1.text then wait for the 5 seconds then continue with the rest of the code? We have in the code around 35 text labels in similar way.
you've blocked the UI thread. use async/await for your delay
ASKER
Any example of code?
Depending on the type of app and situation, you might be able to use a timer, a BackgroundWorker or maybe an Application.DoEvents after setting the .Text. Or some combination thereof.
ASKER
We are using a webapp in asp.net c# but we looked and we can't find anything to fix that for webapp. We looked for hours and we can´t see anything that really works.
Any ideas or example code?
Any ideas or example code?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried this:
on head:
Then in code:
And the "chkbuglabel" is never updated before the sleep and after the sleep take the values of the rest of the code. We just want to have a text on that label before the pause of 5 seconds and after the pause of 5 seconds is not important because that label will take the rest of the script code.
Any ideas?
on head:
<head id="Head1" runat="server">
<script type="text/javascript">
function updateLabelref() {
document.getElementById('chkbuglabel').innerHTML = 'Working...';
}
</script>
Then in code:
StringBuilder refreshlab = new StringBuilder();
refreshlab.Append("<script type='text/javascript'>updateLabelref()</");
refreshlab.Append("script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "changerefresh", refreshlab.ToString());
System.Threading.Thread.Sleep(5000);
And the "chkbuglabel" is never updated before the sleep and after the sleep take the values of the rest of the code. We just want to have a text on that label before the pause of 5 seconds and after the pause of 5 seconds is not important because that label will take the rest of the script code.
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The problem now this code is executed in another button called "initialbuttonclick":
After that code there are more code in the same execution like write a file, execute .bat files and so on that is why is important trigger from that button "initialbuttonclick". I don't enter whole code because there is no need because the problem is before that code.
Now if I use what you mention:
Then I'm executing another different button action and not the initial button I have that trigger all actions ("initialbuttonclick") and I'm not redirecting to the "initialbuttonclick".
What can be done?
StringBuilder refreshlab = new StringBuilder();
refreshlab.Append("<script type='text/javascript'>updateLabelref()</");
refreshlab.Append("script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "changerefresh", refreshlab.ToString());
System.Threading.Thread.Sleep(5000);
After that code there are more code in the same execution like write a file, execute .bat files and so on that is why is important trigger from that button "initialbuttonclick". I don't enter whole code because there is no need because the problem is before that code.
Now if I use what you mention:
<asp:button Id="SubmitButton" runat="server" OnClientClick="updateLabelref()">
Then I'm executing another different button action and not the initial button I have that trigger all actions ("initialbuttonclick") and I'm not redirecting to the "initialbuttonclick".
What can be done?
The OnClientClick attribute can be added to most ASP.NET controls. My previous comment was just a usage example.
Just add it to your button. If you look at the generated page source for the button, you'll see the javascript onclick attribute was added to the button.
Just add it to your button. If you look at the generated page source for the button, you'll see the javascript onclick attribute was added to the button.
ASKER
I could make finally work.
Thank you
Thank you