Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show time taken my function/process in C#.When I click on button show,I want show time in label as 0,and keep on changing like1,2,3,4,5.In button click event writing/processing/executing query ,when that process over I want stop time.How to do that?

What I have tried:

No idea what to do.Does I need to use timer control for this.
Posted
Updated 11-May-16 22:43pm

Try this
this might not gives the 100% result, since some tolerance will be there for Round Trip.

ASPX
ASP.NET
<asp:Button ID="btnTest" OnClientClick="showtime()" runat="server" Text="Test" OnClick="btnTest_Click" />
      <asp:Label Text="text" ID="lblTime" runat="server" />

JAVASCRIPT
JavaScript
<script type="text/javascript">

       function showtime() {
           setInterval(setTime, 1000);
           var lbl = document.getElementById('<%= lblTime.ClientID %>');
           var i = 0;
           function setTime() {
               lbl.innerHTML = i + ' Seconds';
               i++;
           }
       }
   </script>


C#

C#
protected void btnTest_Click(object sender, EventArgs e)
       {
           System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
           watch.Start();
         //  System.Threading.Thread.Sleep(3000); sleeps for 3 seconds
           // Your code..
           watch.Stop();
           lblTime.Text = watch.Elapsed.Seconds + " Seconds";
       }
 
Share this answer
 
Comments
Member 11589429 12-May-16 5:52am    
Hi Karthik,thanks for ur help.That help me lot. setInterval(setTime, 1000) means call setTime() function,after every 1000 miliseconds.
Karthik_Mahalingam 12-May-16 6:06am    
It means for every 1 second, it will call the setTime method
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900