Solved

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

Posted on 2016-09-24
10
52 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 78

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 18
getting id from database 5 24
Events in static methods 3 25
C# TextBox 11 12
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now