How to Delay Calling a function After full page loaded on aspx with c#
Asked Answered
S

2

6

Below is my simple source code, I'm just trying to call that function after page load, but what is happening right now is: it calls that function before page load. Page loads for 5 seconds and, after that, it displays label execution.

protected void Page_Load(object sender, EventArgs e)
{
   display();
}

void display()
{
    Thread.Sleep(5000);
    Label3.Text = "done";
}
Siderolite answered 5/11, 2018 at 6:58 Comment(8)
Maybe try a Timer?Offense
is this webforms?Walrus
Yes it is webform .aspx pages with c#Siderolite
i agree with the Timer answer of @Offense - read this first:robertgreiner.com/2010/06/using-stopwatches-and-timers-in-netWalrus
can you plz give an example of timer because i have used Timer and it is also not working @OffenseSiderolite
when we use Page_LoadCompleate event is also work like Page_Load event..it means it will load page for 5 second and after then it will show the executed label but what i need i just want to see its execution after loading of page.Siderolite
Well there is no option is remaining right now thats why i am using timer now i think Thread . Sleep will not work i dont know why?Siderolite
Any code you execute in the server side is going to run BEFORE the page shows in the browser. This is how the page lifecycle works. You will need to search for another approach: a timer or an ajax call for instance.Tutt
C
2

You should read this document about ASP.NET Page Life Cycle. It actually says what you are trying to do is not possible.

Every code you write in the server side is going to run before the browser renders the page. That means you can not call a function after the browser renders the page, unless you use other approach.

The simplest way you can achieve this is using Javascript and an Ajax call or by using a Timer, which opens a new thread different from the main execution thread. Although, I do not recommend open new threads, because you will lose the control over the execution flow and in a webpage you could end having hundreds of open threads.

Chipmunk answered 5/11, 2018 at 8:1 Comment(2)
Don't use a Timer. How do you know how long the page is going to load? You don't. It's highly variable and will alter from user to user.Squirearchy
I think for the OP it does not matter if it's 4.9 seconds or 5.1 seconds. In any case, I actually recommended not to use Timers in my answer. Just pointed that it is a way to do it, yet not the better way, which for me is clearly the "Ajax" way.Tutt
S
-3

Below Code is Working

protected  void Page_Load(object sender, EventArgs e)
{
    Timer1.Interval = 2000;
    Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
        Label3.Text = "done";
}
Siderolite answered 5/11, 2018 at 7:16 Comment(3)
Your original Thread.Sleep doesn't work because you use it before page_load ends. Start page_load->start Display->wait->end Display->End Page Load. All in one thread, so the page_load does not end before you have done everything else. If you step through your code with debug, you can easily see this.Offense
Are you sure you tested this for asp.net, not for a desktop application? I can't see how this would work, as the page will have already been rendered and sent to the client by the time the timer executes.Stem
This won't do what OP wants. You're delaying the completion of the server-side code but that has no effect on the client-side where the page is rendered by the browser.Johniejohnna

© 2022 - 2024 — McMap. All rights reserved.