Thread safety at application startup

I have an ASP.NET application in which I write this code in the Application_OnStart event:

public virtual void OnStart(HttpApplication httpApplication) { MyClass.PopulateIndices(); } 

Now I know that App_Onstart launched only once, so my question is: I need to add thread safety to this code, for example:

 lock(some object) { MyClass.PopulateIndices(); } 

Is this really lock() ? Can multiple threads run the OnStart application at the same time?

+6
source share
3 answers

It will be called only once. Definitely. You do not need a lock.

From MSDN:

The Application_Start method is called only once during the life of the application loop.

Source: http://msdn.microsoft.com/en-us/library/ms178473.aspx

+9
source

I checked this with some protocols, and Application_Start is executed only once (for example, while Session_Start is executed every time each user starts a session).

you do not need a lock.

+1
source

I recommend you only serviceAutoStartProviders if you are using .NET 4.0 instead:

Autostart ASP.NET Applications (VS 2010 and .NET 4.0 Series)

+1
source

Source: https://habr.com/ru/post/898672/


All Articles