-7

this method calling in page load.

pageLoad:

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

my method:

public void run_after_pageload()
{
                //  code
}

3 Answers3

0

in addition to use QueueBackgroundWorkItem (https://learn.microsoft.com/en-us/dotnet/api/system.web.hosting.hostingenvironment.queuebackgroundworkitem), you could use HttpTaskAsyncHandler, and make use of async, and use await Task.Delay(xxxx).

Something along these lines:

using System;
using System.Web;
using System.Threading.Tasks;

namespace PageLoadWithDelay
{
    public class DelayedLoad : HttpTaskAsyncHandler
    {
        private int _delay = 1 * 1000;
        public override async Task ProcessRequestAsync(HttpContext context)
        {
            var response = context.Response;

            await LongRunningDatabaseOrNetworkTask(....);

            await Task.Delay(_delay);

            await LongRunningDatabaseOrNetworkTask(....);
        }

        public override bool IsReusable
        {
            get { return true; }
        }

        public override void ProcessRequest(HttpContext context)
        {
            throw new Exception("no implementation.");
        }
    }
}

and use this instead of a regular aspx page.

yob
  • 528
  • 4
  • 9
-2

You should have a look at the System.Web.UI.Timer.

Have a look here

Julien P.
  • 35
  • 7
-3

There are two options. Option one would be to use a timer. This would allow you to specify the time period and if need be the interval. If you are only running it once you can just disable the timer

Option two. use

 System.Threading.Thread.Sleep(10000);

where the number in the sleep function is the time to wait in ms. 1000 ms = 1 second

Shon
  • 486
  • 4
  • 9
  • You can use Application.DoEvents() to force the loaded objects to be displayed first but it is not the best option. I personally would use the timer but the sleep may be useful elsewhere. – Shon Jun 03 '16 at 19:03
  • do you have an example of using `Application.DoEvents` in asp.net? – stuartd Jun 03 '16 at 19:05
  • When multi threading and i want to push results to the GUI before all of he processing is completed so that another process can use them allowing multiple threads to process at the same time – Shon Jun 03 '16 at 19:08
  • @Shon , i use the timer and i have only running it once , where is put timer.enable=false ? – Saeed Mohammadi Jun 03 '16 at 19:17
  • @stuartd , no i have'nt example of using Application.DoEvents. – Saeed Mohammadi Jun 03 '16 at 19:21
  • in your timer code. You can put it at the beginning so that if the function you want to run will take longer than the interval it will not run again. Also, that would be an example of where Appliction.DoEvents would be used. Function takes 20 seconds but interval is only 5 seconds then you want to stop the timer before calling the function – Shon Jun 03 '16 at 19:23