的&QUOT跨线程使用; HttpContext.Current"财产和相关的东西 [英] The cross-thread usage of "HttpContext.Current" property and related things

查看:131
本文介绍了的&QUOT跨线程使用; HttpContext.Current"财产和相关的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自以下的读取;基本ASP.NET在C#>下面的语句例如:

I read from < Essential ASP.NET with Examples in C# > the following statement:

了解另一种有用的特性是静态电流特性
  的HttpContext类。此属性总是指向当前
  请求的HttpContext类的一个实例被服务。这个
  如果您正在编写将被用于辅助类可以方便
  从网页或其他管道类,可能需要访问
  背景无论出于何种原因。通过静态电流属性
  检索上下文,就可以避免传递一个引用它
  辅助类。例如,清单4-1所示的类使用
  上下文的当前属性来访问查询字符串和打印
  东西电流响应缓冲。 请注意,此静态
  属性正确初始化,调用者必须在执行
  原始请求的线程,因此,如果你已经产生了额外的线程
  一个请求期间执行的工作,你必须小心提供访问
  上下文类自己。

Another useful property to know about is the static Current property of the HttpContext class. This property always points to the current instance of the HttpContext class for the request being serviced. This can be convenient if you are writing helper classes that will be used from pages or other pipeline classes and may need to access the context for whatever reason. By using the static Current property to retrieve the context, you can avoid passing a reference to it to helper classes. For example, the class shown in Listing 4-1 uses the Current property of the context to access the QueryString and print something to the current response buffer. Note that for this static property to be correctly initialized, the caller must be executing on the original request thread, so if you have spawned additional threads to perform work during a request, you must take care to provide access to the context class yourself.

我想知道有关加粗部分的根本原因,一件事引出了另一个,这是我的想法:

I am wondering about the root cause of the bold part, and one thing leads to another, here is my thoughts:

我们知道,一个进程可以有多个线程。这些线程都有自己的堆栈,分别。这些线程也可以使用一个共享内存区,堆。

We know that a process can have multiple threads. Each of these threads have their own stacks, respectively. These threads also have access to a shared memory area, the heap.

堆栈的话,按照我的理解,是一种所有线程的上下文存储在哪里。对于一个线程在堆中访问一些它必须为指针,指针存储它的堆栈中。

The stack then, as I understand it, is kind of where all the context for that thread is stored. For a thread to access something in the heap it must use a pointer, and the pointer is stored on its stack.

所以,当我们做一些跨线程调用,我们必须确保所有必要的上下文信息从呼叫者线程的堆栈到被叫方线程的堆栈传递。

So when we make some cross-thread calls, we must make sure that all the necessary context info is passed from the caller thread's stack to the callee thread's stack.

但我不能肯定,如果我没有犯错。

But I am not quite sure if I made any mistake.

任何评论都将深深AP preciated。

Any comments will be deeply appreciated.

感谢。

下面堆仅限于用户栈

推荐答案

有四样东西一起工作,使你问有关的行为:

There are four things working together to cause the behavior you are asking about:


  1. 的HttpContext是一个实例对象,其引用可以在 HttpContext.Current 中找到

  2. 主题也是一个实例对象的引用可以在 Thread.CurrentThread 中找到

  3. Thread.CurrentThread 是静态的,而是引用不同的对象每个线程

  4. HttpContext.Current 实际上指向 Thread.CurrentThread.ExecutionContext.IllogicalCallContext.HostContext

  1. HttpContext is an instance object whose reference can be found in HttpContext.Current
  2. Thread is also an instance object whose reference can be found in Thread.CurrentThread
  3. Thread.CurrentThread is static but references a different Thread object in every thread
  4. HttpContext.Current actually points to Thread.CurrentThread.ExecutionContext.IllogicalCallContext.HostContext

结论,我们可以从上述吉文斯绘制:

Conclusions we can draw from the above givens:


  1. 因为的HttpContext 是一个实例对象,而不是静态的,我们需要它的引用来访问它

  2. 因为 HttpContext.Current 实际指向一个属性 Thread.CurrentThread ,修改 Thread.CurrentThread 来一个不同的对象可能会改变 HttpContext.Current

  3. 因为线程切换时 Thread.CurrentThread '的变化, HttpContext.Current 也切换线程(在这个时候改变案例 HttpContext.Current 变空)。

  1. Because HttpContext is an instance object and not static we need its reference to access it
  2. Because HttpContext.Current actually points to a property on Thread.CurrentThread, changing Thread.CurrentThread to a different object will likely change HttpContext.Current
  3. Because Thread.CurrentThread' changes when switching threads, HttpContext.Current also changes when switching threads (in this case HttpContext.Current becomes null).

这瞻一起,是什么原因导致 HttpContext.Current 来在一个新线程不工作?在 Thread.CurrentThread 引用的变化,切换线程时恰好,改变了 HttpContext.Current 引用,prevents我们从得到我们想要的HttpContext实例。

Bringing this all together, what causes HttpContext.Current to not work in a new Thread? The Thread.CurrentThread reference change, which happens when switching threads, changes the HttpContext.Current reference, which prevents us from getting to the HttpContext instance we want.

要重申的是,这只神奇的东西怎么回事是 Thread.CurrentThread 引用的每个线程不同的对象。 HttpContext的工作就像任何其他的实例对象。由于在相同的AppDomain线程可以引用相同的对象,我们所要做的就是为传递给HttpContext的新线程的引用。没有上下文信息加载或类似的东西。 (有一些相当严重的潜在的陷阱周围的HttpContext传递给其他线程,但没有从做它prevent你)。

To reiterate, the only magic thing going on here is Thread.CurrentThread referencing a different object in every Thread. HttpContext works just like any other instance object. Since threads in the same AppDomain can reference the same objects, all we have to do is pass a reference for HttpContext to our new thread. There is no context info to load or anything like that. (there are some fairly serious potential gotchas with passing around HttpContext to other threads but nothing to prevent you from doing it).

一个最后几个旁注我碰到,同时研究:

A few final side notes I came across while researching:


  1. 在某些情况下,线程的执行上下文是流(复制)从一个主题到另一个。那么为什么不HttpContext的'流动'到我们新的主题?因为HttpContext的没有实现ILogicalThreadAffinative接口。存储在ExecutionContext中一个类是否实现ILogicalThreadAffinative只流了出来。

  1. In some cases a Thread's ExecutionContext is 'flowed' (copied) from one Thread to another. Why then is HttpContext not 'flowed' to our new Thread? Because HttpContext doesn't implement the ILogicalThreadAffinative interface. A class stored in the ExecutionContext is only flowed if it implements ILogicalThreadAffinative.

如何ASP.NET移动的HttpContext从主题到线程(线程敏捷),如果它不流动?我不能完全肯定,但它看起来像它可能通过它在 HttpApplication.OnThreadEnter()

How does ASP.NET move HttpContext from Thread to Thread (Thread-Agility) if it isn't flowed? I'm not entirely sure, but it looks like it might pass it in HttpApplication.OnThreadEnter().

这篇关于的&QUOT跨线程使用; HttpContext.Current&QUOT;财产和相关的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆