Solved

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

Posted on 2016-09-24
10
113 Views
Last Modified: 2016-09-29
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
Comment
Question by:Alex E.
  • 5
  • 4
10 Comments
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 41813988
you've blocked the UI thread.  use async/await for your delay
0
 

Author Comment

by:Alex E.
ID: 41814050
Any example of code?
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41814369
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Alex E.
ID: 41814735
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
 
LVL 12

Assisted Solution

by:funwithdotnet
funwithdotnet earned 500 total points
ID: 41814942
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
 

Author Comment

by:Alex E.
ID: 41816074
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
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 500 total points
ID: 41821017
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
 

Author Comment

by:Alex E.
ID: 41821037
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
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41821058
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
 

Author Closing Comment

by:Alex E.
ID: 41822384
I could make finally work.

Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question