Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Anything wrong with setting HttpContext.Current in a parallel thread?

I'm using a library that relies on HttpContext.Current. The library is Facebook C# SDK, but my question should apply in other scenarios as well. I'd like to use this library from inside of a parallel thread. However, HttpContext.Current is not available in the parallel thread, so I'm thinking about caching it to a local variable and then setting it in the parallel thread like this:

var httpContext = HttpContext.Current;
Parallel.ForEach(items, item => {

    try {

        HttpContext.Current = httpContext;

        // Call a method that relies on HttpContext.Current

    } finally {
        HttpContext.Current = null;
    }
});

Do you foresee anything wrong with this? Are there any repercussions to doing this?

like image 808
Johnny Oshika Avatar asked Oct 21 '11 15:10

Johnny Oshika


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C in C?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

For me seems ok. The use of try ... finally is a good point too because thread can be reused and you can keep the context alive for long times, avoiding garbage collection. Don't think there is another way to solve this.

Be careful however that the api you are calling does not create problems in this multithreading environment. Not all code is thread safe performing write operations or read operations that involve writing\reading some cached value.

Be careful also that field values can not be propagated correctly and\or in time from one thread to another if they are not volatile or if System.Threading.Interlocked is not used! This may create you problems, especially in release builds.

You can however use Thread.MemoryBarrier or lock, search on the web for this annoying (but inevitable) issue.

like image 130
Salvatore Previti Avatar answered Sep 24 '22 18:09

Salvatore Previti