加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 应用程序启动时的线程安全性

发布时间:2020-12-15 08:07:47 所属栏目:百科 来源:网络整理
导读:我有一个ASP.NET应用程序,我在Application_OnStart事件中编写此代码: public virtual void OnStart(HttpApplication httpApplication){ MyClass.PopulateIndices();} 现在,我知道App_Onstart只被触发一次,所以我的问题是:我是否需要在此代码中添加线程安全
我有一个ASP.NET应用程序,我在Application_OnStart事件中编写此代码:
public virtual void OnStart(HttpApplication httpApplication)
{
    MyClass.PopulateIndices();
}

现在,我知道App_Onstart只被触发一次,所以我的问题是:我是否需要在此代码中添加线程安全性,例如:

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

这个锁()真的需要吗?多个线程可以同时触发App OnStart吗?

解决方法

它只会被调用一次.当然.你不需要任何锁定.

来自MSDN:

The Application_Start method is called only one time during the life
cycle of an application.

资料来源:http://msdn.microsoft.com/en-us/library/ms178473.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读