Link to home
Start Free TrialLog in
Avatar of Alex E.
Alex E.

asked on

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.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

you've blocked the UI thread.  use async/await for your delay
Avatar of Alex E.
Alex E.

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.
Avatar of Alex E.

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?
SOLUTION
Avatar of funwithdotnet
funwithdotnet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex E.

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex E.

ASKER

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?
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.
Avatar of Alex E.

ASKER

I could make finally work.

Thank you