[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

ASP.NET C# System.Threading.Thread.Sleep and text label problem

I have part of this code:

label1.text = "System Working Please Wait...";
System.Threading.Thread.Sleep(5000);

Open in new window


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.
0
Alex E.
Asked:
Alex E.
  • 5
  • 4
2 Solutions
 
David Johnson, CD, MVPOwnerCommented:
you've blocked the UI thread.  use async/await for your delay
0
 
Alex E.Author Commented:
Any example of code?
0
 
funwithdotnetCommented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Alex E.Author Commented:
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?
0
 
funwithdotnetCommented:
In a web app, all server-side code is executed and the life cycle is completed before the page is refreshed.

All you need to do is update the label client-side. For example, the user presses a button that cause a postback, but also executes javascript that updates the label text before posting back to the server.
0
 
Alex E.Author Commented:
I tried this:

on head:

<head id="Head1" runat="server">

<script type="text/javascript">
    function updateLabelref() {
        document.getElementById('chkbuglabel').innerHTML = 'Working...';
    }
</script>

Open in new window


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);

Open in new window



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?
0
 
funwithdotnetCommented:
Try something like:
<asp:button Id="SubmitButton" runat="server" OnClientClick="updateLabelref()">

Open in new window


Once the postback happens, there is no longer a page. When the response is complete, a new page is returned. The script in the code-behind doesn't exist until after the sleep and the response.

Don't forget to reset the label text in the postback if it's a server-side control and the page is using viewstate.
0
 
Alex E.Author Commented:
The problem now this code is executed in another button called "initialbuttonclick":

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);

Open in new window


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()">

Open in new window


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?
0
 
funwithdotnetCommented:
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.
0
 
Alex E.Author Commented:
I could make finally work.

Thank you
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now