从新线程将对象加载到缓存中

发布于 2024-11-02 17:31:28 字数 1358 浏览 1 评论 0原文

我正在尝试使用 BackgroundWorker 类启动一个新线程,在网站启动时将大量对象加载到缓存中。

到目前为止我的代码:

private void PreLoadCachedSearches()
{
    var worker = new BackgroundWorker() { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
    worker.DoWork += new DoWorkEventHandler(DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
    worker.RunWorkerAsync();
}

private static void DoWork(object sender, DoWorkEventArgs e)
{
    // Do the cache loading...
    var x = HttpContext.Current.Cache; // BUT the Cache is now null!!!!
}

private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Logging?
}

我将代码放在 Global.asax.cs 中,并在 Application_Start 事件期间调用 PreLoadCachedSearches:新线程已启动,但每当它失败时它尝试通过 HttpContext.Current.Cache 访问缓存,该缓存为 null。我假设 HttpContext 在我用 BackgroundWorker 启动的新线程中不存在/不可用。 我还尝试将代码移至单独的页面并手动启动线程,而不是通过 Application_Start 事件 - 同样的问题。
如果我在 Web 应用程序的上下文中调用缓存加载代码(即无线程),它就可以正常工作。

  • 我该如何解决这个问题?传入对主线程缓存的引用或以某种方式访问​​它?

这个问题是上一个问题的延续,ASP.NET 中的异步任务

I'm trying to use the BackgroundWorker class to start a new thread which loads a large number of objects into the cache when the website is started.

My code so far:

private void PreLoadCachedSearches()
{
    var worker = new BackgroundWorker() { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
    worker.DoWork += new DoWorkEventHandler(DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
    worker.RunWorkerAsync();
}

private static void DoWork(object sender, DoWorkEventArgs e)
{
    // Do the cache loading...
    var x = HttpContext.Current.Cache; // BUT the Cache is now null!!!!
}

private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Logging?
}

I put the code in Global.asax.cs and call PreLoadCachedSearches during the Application_Start event: The new thread is started, but it fails whenever it tries to access the cache via HttpContext.Current.Cache which is null. I assume HttpContext doesn't exist/isn't available in the new thread I'm kicking off with the BackgroundWorker.
I've also tried moving the code to a separate page and start the thread manually rather than via the Application_Start event - same problem.
If I call my cache-loading code in the context of the web application (i.e. no threading) it works just fine.

  • How do I work around this? Pass in a reference to the cache of the main thread or access it somehow?

This question is a continuation of this previous question, Asynchronous task in ASP.NET.

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

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

发布评论

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

评论(2

李白 2024-11-09 17:31:28

您没有 HttpContext,因为该线程不参与服务 Http 请求。

尝试 HttpRuntime.Cache

You don't have an HttpContext because the thread isn't involved in servicing an Http Request.

Try HttpRuntime.Cache

天涯沦落人 2024-11-09 17:31:28

您可以通过传递 HttpContex.Current 作为参数来实现;

private void PreLoadCachedSearches()
{
    var worker = new BackgroundWorker() { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
    worker.DoWork += new DoWorkEventHandler(DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
    worker.RunWorkerAsync(HttpContext.Current);
}

private static void DoWork(object sender, DoWorkEventArgs e)
{
    HttpContext.Current = (HttpContext)e.Argument;
    var x = HttpContext.Current.Cache;
}

private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Logging?
}

You can do it by passing HttpContex.Current as parameter;

private void PreLoadCachedSearches()
{
    var worker = new BackgroundWorker() { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
    worker.DoWork += new DoWorkEventHandler(DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
    worker.RunWorkerAsync(HttpContext.Current);
}

private static void DoWork(object sender, DoWorkEventArgs e)
{
    HttpContext.Current = (HttpContext)e.Argument;
    var x = HttpContext.Current.Cache;
}

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