字典<字符串 ,字符串>在 Global.asax 中使用 Application.Add 定义

发布于 11-02 17:56 字数 855 浏览 2 评论 0原文

我在 ASP.NET 4.0 Web 应用程序的 Global.asax 中有以下代码,

protected void Application_Start(object sender, EventArgs e)
{
  Dictionary<string, string> AllCompanyList = new Dictionary<string, string>();
  Application.Add("GlobalCompanyList", AllCompanyList);

  Thread ProcessCompanyThread = new Thread(new ParameterizedThreadStart(CompanyThread.Start));
  ProcessCompanyThread.IsBackground = true;
  ProcessCompanyThread.Start(AllCompanyList);
}

我在另一个页面上通过

Dictionary<string, string> AllCompanyList = (Dictionary<string, string>)HttpContext.Current.Application["GlobalCompanyList"];
  1. 首先访问它,是“GlobalCompanyList” IIS 中只有一个实例 生命周期?

  2. 第二个是“GlobalCompanyList” 可以线程安全地访问或修改 在 ProcessCompanyThread 中?如果是的话 不是那么我必须做什么才能做到 线程安全吗?

感谢您的帮助。

I have below code at my ASP.NET 4.0 web application's Global.asax

protected void Application_Start(object sender, EventArgs e)
{
  Dictionary<string, string> AllCompanyList = new Dictionary<string, string>();
  Application.Add("GlobalCompanyList", AllCompanyList);

  Thread ProcessCompanyThread = new Thread(new ParameterizedThreadStart(CompanyThread.Start));
  ProcessCompanyThread.IsBackground = true;
  ProcessCompanyThread.Start(AllCompanyList);
}

I access it on another page by

Dictionary<string, string> AllCompanyList = (Dictionary<string, string>)HttpContext.Current.Application["GlobalCompanyList"];
  1. First of all, is "GlobalCompanyList"
    only has one instance in IIS's
    lifetime?

  2. Second, is "GlobalCompanyList"
    thread safe to be access or modify
    in ProcessCompanyThread? If it is
    not then what must i do to make it
    thread safe?

Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我2024-11-09 17:56:23

1) AllCompanyList 在应用程序实例的生命周期内只有一个实例。 IIS 可以并且将会随意启动和停止应用程序实例(甚至可以同时运行多个应用程序实例),具体取决于其配置方式。

GlobalCompanyList 本质上不是线程安全的。您可以通过锁定它来安全地读取和写入它。

lock(AllCompanyList)
{
    //make changes
}

线程安全和并发性很复杂 - 即使上面的内容也不是完全安全的,因为它不能阻止某人尝试:

Application["GlobalCompanyList"].Add()...

这是在锁之外的。如果需要线程安全,最好的方法是将您的公司列表封装在线程安全对象中。

1) AllCompanyList will only have one instance for the lifetime of the Application Instance. IIS can and will start and stop application instances at will (and even have more than application instance running at one time) depending on how it is configured.

GlobalCompanyList is not inherently thread safe. You can make reading and writing to it safe by locking it.

lock(AllCompanyList)
{
    //make changes
}

Thread safety and concurrency is complicated - even the above is not totally safe because it doesn't prevent someone from attempting:

Application["GlobalCompanyList"].Add()...

which is outside of a lock. The best approach if thread safety is needed would be to encapsulate your company list in a thread safe object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文