Automatic code execution in ASP.NET

My task is to create an ASP.NET application with a piece of code that will automatically run every half hour. This code should add an entry to the file every half hour. It should not depend on user requests (regardless of whether they occur or not).
 1) I write my code in the Application.Start method of the Global.asax file. I'm right?
 2) Do I have anything to do with host settings (IIS) (e.g. change permissions to write a file, etc.)?

I already tried just putting the code to write to the file in a loop in the Application.Start method, and then just copied the project directory to the server. However, this did not work.

+3
source share
4 answers

ASP.NET, , , ASP.NET . , , , , .

, , - , -, , Windows.

+1

, , . Application.Start .

void Application_Start(...)
{
    Thread thread = new Thread(CronThread);
    thread.IsBackground = true;
    thread.Start();
}

private void CronThread()
{
    while(true)
    {
        Thread.Sleep( TimeSpan.FromMinutes( 30 ) );
        // Do something every half hour
    }
}
+4

ASP.Net(. Pauls , ), , , (.. ) - ASP.net , " " .

, , , , Windows, .

. , Windows #:

+3
source

No, this code will not be used in your Application_Start method. This method is called when the application starts or when it is reset and has nothing to do with "every half hour."

The best way to handle this, BY FAR, is to create a separate application, either on the same computer or on another computer that has access to the "file" (whatever that means). ASP.NET is not configured for timer events, and any attempt to do this in ASP.NET is likely to affect the performance of your site.

+1
source

All Articles