Inheritable thread local

Jul 30, 2008 at 9:46 PM
Hello,

Often I find myself facing the problem that I believe its common, especially to former Java Developers. In Java I often used  Thread variables. The point is that in Java coders can choose between a "simple" thread var, and an "inheritable" thread var, being the late available in the child threads.
However in .NET i did not find, until now, a solution to this situation, that often comes in hand. Is there any way to "mark" a thread var as being inheritable?
I am aware that one can use ParameterizedThreadStart, but I'd prefer it to be automatic for threads coming off the thread pool.

Sample Scenario:

Web Application -> One thread per request. A thread variable holds the current logged on user.
One of these threads summons another thread to perform some task. In the child thread I want the variable to still available.
The details of accessing the variable are not important, just assume I have a public static variable in a class.

Is this problem addressed by any means on Smart Thread Pool? If not, do the developers/readers of this discussion aware of an existent solution? If not, anybody has an idea were to start building one?

Thank you very much.
Coordinator
Jul 31, 2008 at 4:13 PM
Hi,

To do what you need create an object with the request (MyWebRequest) and then start a thread with a method in MyWebRequest.
If you need another thread call to another/the same method within the instance of MyWebRequest and it will have the same variables.
This is the simplest way.

The SmartThreadPool has an option to pass the HTTP context between the caller and the executing thread.
When the web request handler is called it has an HTTP context. If you queue a work item into the STP at this point
the HTTP context will be stored, and when the work item is executed it can use the HTTP context.
To use this feature you need to set the STPStartInfo.UseCallerCallContext to true.

Ami
Mar 2, 2011 at 5:07 AM
Edited Mar 2, 2011 at 5:08 AM

Hi,

I Have use this STP in my application for Printing Purpose. What it does is When something is need to be printed it will added to the Thread pool.  This dosn't work. My operating system is Windows 7.

When I added to the STP it successfully added and no exceptions. But printing is not working. If I call print  method without STP, It prints.

Can any body has such experience ?

 

 

        public void PrintUsingThreadWithCallerContext(string FileName)
        {
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.UseCallerCallContext = true;

            SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo);
            
            TestPrinter printer = new TestPrinter();
            smartThreadPool.QueueWorkItem(printer.Print, FileName);
        }

 

Printing Functions written in a VB.NET library.

 

 ' Print the file.
    Public Sub Print(ByVal filePath As String)
        Try
            streamToPrint = New StreamReader(filePath)
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                pd.PrinterSettings.PrinterName = "PDFCreator"
                'pd.PrinterSettings.
                AddHandler pd.PrintPage, AddressOf pd_PrintPage


                'Print the document.
                pd.Print()
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            Throw ex


        End Try
    End Sub