Synchronization problem

Discussion in 'ASP .Net' started by Johan Öhrn, Nov 12, 2008.

  1. Johan Öhrn

    Johan Öhrn Guest

    Hi,

    I'm working with an asp.net 2.0 application.

    The application I'm working with come with it's own assemblies which calls
    out to code in my assembly.
    It may call out to my code during a request, i.e. I click on a link in the
    application or when the application does some background processing in a
    different thread.

    My problem is when my code gets called from a background thread. The thread
    itself is created in the following manner:
    ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkThread));
    Note that this code is out of my control.

    This thread (hereby refered to as job) call one of my functions to get a
    collection of items which the job operates upon. The same job may call my
    function any number of times during it's execution. The problem I have is
    that the application (ouf of my control) throws an exception if my function
    returns different data between from one call to the next. So I must somehow
    make sure that I always return the same data to the job.

    Sometimes as I mentioned my code is executed in the context of the current
    request. I must still make sure that my function return the same data during
    the whole request. In this scenario I can store a cached version of the data
    in the HttpContext.Items collection the first time it's asked for and then
    return that cached data for any subsequent call to my function.

    So my problem is that I can't find a working solution for the scenario where
    my function is called from a background thread because I don't have access
    to the HttpContext. I've tried to store data in the Thread Local Store (TLS)
    but it turns out that the TLS is not cleared when the threads are reused by
    the ThreadPool.

    If anyone have an idea how I can fix this issue I'd be really glad!


    I'm attaching some pseudo code that illustrates the problem:
    (Look here for a cleaner version of the code:
    http://rafb.net/p/oKxBWB39.html)

    namespace Product.Proprietary.Assembly.OutOfMyControl

    {

    public class SomeWorkToBeDone

    {

    public void DoWork()

    {

    ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkThread));

    }



    private void WorkThread(object obj)

    {

    MyOwn.Assembly.SomeHelperClass helper = new
    MyOwn.Assembly.SomeHelperClass();



    ArrayList items = helper.GetItems();



    int unknownNumberOfTimesToGetItems = new Random().Next();

    for (int i = 0; i < unknownNumberOfTimesToGetItems; i++)

    {

    if (helper.GetItems().Count != items.Count)

    throw new Exception("You didn't return the same items as
    the last call. I throw an exception!");

    }

    }

    }

    }



    namespace MyOwn.Assembly

    {

    public class SomeHelperClass

    {

    public ArrayList GetItems()

    {

    // I need to make sure I return the same items for the rest of
    this context!

    // If i'm not called from a background thread I have access to
    the HttpContext object in which I can cache the items and they are magically
    removed when the request ends.

    if (HttpContext.Current != null)

    {

    ArrayList entries = HttpContext.Current.Items[GetHashCode()]
    as ArrayList;

    if (entries == null)

    {

    HttpContext.Current.Items[GetHashCode()] = entries = new
    ArrayList(BackEndStore.GetItems());

    }

    return entries;

    }

    else

    {

    // No HttpContext because I was called from a background
    thread (a job) - Bummer! What do I do now???

    // I know! Pray that the backend store will return the same
    items each time for the rest of this job, i.e. rest of the execution of the
    thread before it's reused.

    return BackEndStore.GetItems();

    }

    }

    }





    public class BackEndStore

    {

    public static ArrayList GetItems()

    {

    // I'm not in control of when items are changed in the backend
    store!

    ArrayList list = new ArrayList();

    int unknownNumberOfItems = new Random().Next();

    for (int i = 0; i < unknownNumberOfItems; i++)

    list.Add(1);

    return list;

    }

    }

    }
    Johan Öhrn, Nov 12, 2008
    #1
    1. Advertising

  2. Johan Öhrn

    bruce barker Guest

    unfortunately, .net failed to provide a factory model for the thread pool,
    and there are no hooks/events on dequeueing an thread.

    your 3rd party app seems to have a design flaw. they should pass some
    content information when they make the callback. you should contact them.


    -- bruce (sqlwork.com)


    "Johan Öhrn" wrote:

    > Hi,
    >
    > I'm working with an asp.net 2.0 application.
    >
    > The application I'm working with come with it's own assemblies which calls
    > out to code in my assembly.
    > It may call out to my code during a request, i.e. I click on a link in the
    > application or when the application does some background processing in a
    > different thread.
    >
    > My problem is when my code gets called from a background thread. The thread
    > itself is created in the following manner:
    > ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkThread));
    > Note that this code is out of my control.
    >
    > This thread (hereby refered to as job) call one of my functions to get a
    > collection of items which the job operates upon. The same job may call my
    > function any number of times during it's execution. The problem I have is
    > that the application (ouf of my control) throws an exception if my function
    > returns different data between from one call to the next. So I must somehow
    > make sure that I always return the same data to the job.
    >
    > Sometimes as I mentioned my code is executed in the context of the current
    > request. I must still make sure that my function return the same data during
    > the whole request. In this scenario I can store a cached version of the data
    > in the HttpContext.Items collection the first time it's asked for and then
    > return that cached data for any subsequent call to my function.
    >
    > So my problem is that I can't find a working solution for the scenario where
    > my function is called from a background thread because I don't have access
    > to the HttpContext. I've tried to store data in the Thread Local Store (TLS)
    > but it turns out that the TLS is not cleared when the threads are reused by
    > the ThreadPool.
    >
    > If anyone have an idea how I can fix this issue I'd be really glad!
    >
    >
    > I'm attaching some pseudo code that illustrates the problem:
    > (Look here for a cleaner version of the code:
    > http://rafb.net/p/oKxBWB39.html)
    >
    > namespace Product.Proprietary.Assembly.OutOfMyControl
    >
    > {
    >
    > public class SomeWorkToBeDone
    >
    > {
    >
    > public void DoWork()
    >
    > {
    >
    > ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkThread));
    >
    > }
    >
    >
    >
    > private void WorkThread(object obj)
    >
    > {
    >
    > MyOwn.Assembly.SomeHelperClass helper = new
    > MyOwn.Assembly.SomeHelperClass();
    >
    >
    >
    > ArrayList items = helper.GetItems();
    >
    >
    >
    > int unknownNumberOfTimesToGetItems = new Random().Next();
    >
    > for (int i = 0; i < unknownNumberOfTimesToGetItems; i++)
    >
    > {
    >
    > if (helper.GetItems().Count != items.Count)
    >
    > throw new Exception("You didn't return the same items as
    > the last call. I throw an exception!");
    >
    > }
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    > namespace MyOwn.Assembly
    >
    > {
    >
    > public class SomeHelperClass
    >
    > {
    >
    > public ArrayList GetItems()
    >
    > {
    >
    > // I need to make sure I return the same items for the rest of
    > this context!
    >
    > // If i'm not called from a background thread I have access to
    > the HttpContext object in which I can cache the items and they are magically
    > removed when the request ends.
    >
    > if (HttpContext.Current != null)
    >
    > {
    >
    > ArrayList entries = HttpContext.Current.Items[GetHashCode()]
    > as ArrayList;
    >
    > if (entries == null)
    >
    > {
    >
    > HttpContext.Current.Items[GetHashCode()] = entries = new
    > ArrayList(BackEndStore.GetItems());
    >
    > }
    >
    > return entries;
    >
    > }
    >
    > else
    >
    > {
    >
    > // No HttpContext because I was called from a background
    > thread (a job) - Bummer! What do I do now???
    >
    > // I know! Pray that the backend store will return the same
    > items each time for the rest of this job, i.e. rest of the execution of the
    > thread before it's reused.
    >
    > return BackEndStore.GetItems();
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    >
    >
    > public class BackEndStore
    >
    > {
    >
    > public static ArrayList GetItems()
    >
    > {
    >
    > // I'm not in control of when items are changed in the backend
    > store!
    >
    > ArrayList list = new ArrayList();
    >
    > int unknownNumberOfItems = new Random().Next();
    >
    > for (int i = 0; i < unknownNumberOfItems; i++)
    >
    > list.Add(1);
    >
    > return list;
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    bruce barker, Nov 12, 2008
    #2
    1. Advertising

  3. Johan Öhrn

    Johan Öhrn Guest

    Hi,

    Thanks for the bad news :)

    / Johan

    "bruce barker" <> wrote in message
    news:...
    > unfortunately, .net failed to provide a factory model for the thread pool,
    > and there are no hooks/events on dequeueing an thread.
    >
    > your 3rd party app seems to have a design flaw. they should pass some
    > content information when they make the callback. you should contact them.
    >
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Johan Öhrn" wrote:
    >
    >> Hi,
    >>
    >> I'm working with an asp.net 2.0 application.
    >>
    >> The application I'm working with come with it's own assemblies which
    >> calls
    >> out to code in my assembly.
    >> It may call out to my code during a request, i.e. I click on a link in
    >> the
    >> application or when the application does some background processing in a
    >> different thread.
    >>
    >> My problem is when my code gets called from a background thread. The
    >> thread
    >> itself is created in the following manner:
    >> ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkThread));
    >> Note that this code is out of my control.
    >>
    >> This thread (hereby refered to as job) call one of my functions to get a
    >> collection of items which the job operates upon. The same job may call my
    >> function any number of times during it's execution. The problem I have is
    >> that the application (ouf of my control) throws an exception if my
    >> function
    >> returns different data between from one call to the next. So I must
    >> somehow
    >> make sure that I always return the same data to the job.
    >>
    >> Sometimes as I mentioned my code is executed in the context of the
    >> current
    >> request. I must still make sure that my function return the same data
    >> during
    >> the whole request. In this scenario I can store a cached version of the
    >> data
    >> in the HttpContext.Items collection the first time it's asked for and
    >> then
    >> return that cached data for any subsequent call to my function.
    >>
    >> So my problem is that I can't find a working solution for the scenario
    >> where
    >> my function is called from a background thread because I don't have
    >> access
    >> to the HttpContext. I've tried to store data in the Thread Local Store
    >> (TLS)
    >> but it turns out that the TLS is not cleared when the threads are reused
    >> by
    >> the ThreadPool.
    >>
    >> If anyone have an idea how I can fix this issue I'd be really glad!
    >>
    >>
    >> I'm attaching some pseudo code that illustrates the problem:
    >> (Look here for a cleaner version of the code:
    >> http://rafb.net/p/oKxBWB39.html)
    >>
    >> namespace Product.Proprietary.Assembly.OutOfMyControl
    >>
    >> {
    >>
    >> public class SomeWorkToBeDone
    >>
    >> {
    >>
    >> public void DoWork()
    >>
    >> {
    >>
    >> ThreadPool.QueueUserWorkItem(new
    >> WaitCallback(this.WorkThread));
    >>
    >> }
    >>
    >>
    >>
    >> private void WorkThread(object obj)
    >>
    >> {
    >>
    >> MyOwn.Assembly.SomeHelperClass helper = new
    >> MyOwn.Assembly.SomeHelperClass();
    >>
    >>
    >>
    >> ArrayList items = helper.GetItems();
    >>
    >>
    >>
    >> int unknownNumberOfTimesToGetItems = new Random().Next();
    >>
    >> for (int i = 0; i < unknownNumberOfTimesToGetItems; i++)
    >>
    >> {
    >>
    >> if (helper.GetItems().Count != items.Count)
    >>
    >> throw new Exception("You didn't return the same items
    >> as
    >> the last call. I throw an exception!");
    >>
    >> }
    >>
    >> }
    >>
    >> }
    >>
    >> }
    >>
    >>
    >>
    >> namespace MyOwn.Assembly
    >>
    >> {
    >>
    >> public class SomeHelperClass
    >>
    >> {
    >>
    >> public ArrayList GetItems()
    >>
    >> {
    >>
    >> // I need to make sure I return the same items for the rest
    >> of
    >> this context!
    >>
    >> // If i'm not called from a background thread I have access
    >> to
    >> the HttpContext object in which I can cache the items and they are
    >> magically
    >> removed when the request ends.
    >>
    >> if (HttpContext.Current != null)
    >>
    >> {
    >>
    >> ArrayList entries =
    >> HttpContext.Current.Items[GetHashCode()]
    >> as ArrayList;
    >>
    >> if (entries == null)
    >>
    >> {
    >>
    >> HttpContext.Current.Items[GetHashCode()] = entries =
    >> new
    >> ArrayList(BackEndStore.GetItems());
    >>
    >> }
    >>
    >> return entries;
    >>
    >> }
    >>
    >> else
    >>
    >> {
    >>
    >> // No HttpContext because I was called from a background
    >> thread (a job) - Bummer! What do I do now???
    >>
    >> // I know! Pray that the backend store will return the
    >> same
    >> items each time for the rest of this job, i.e. rest of the execution of
    >> the
    >> thread before it's reused.
    >>
    >> return BackEndStore.GetItems();
    >>
    >> }
    >>
    >> }
    >>
    >> }
    >>
    >>
    >>
    >>
    >>
    >> public class BackEndStore
    >>
    >> {
    >>
    >> public static ArrayList GetItems()
    >>
    >> {
    >>
    >> // I'm not in control of when items are changed in the
    >> backend
    >> store!
    >>
    >> ArrayList list = new ArrayList();
    >>
    >> int unknownNumberOfItems = new Random().Next();
    >>
    >> for (int i = 0; i < unknownNumberOfItems; i++)
    >>
    >> list.Add(1);
    >>
    >> return list;
    >>
    >> }
    >>
    >> }
    >>
    >> }
    >>
    >>
    >>
    Johan Öhrn, Nov 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francis
    Replies:
    1
    Views:
    614
    Ronald Hecht
    Apr 27, 2004
  2. Sami Lakka

    Synchronization Problem

    Sami Lakka, Dec 4, 2003, in forum: Java
    Replies:
    1
    Views:
    363
  3. Vincent Lascaux

    Thread synchronization problem

    Vincent Lascaux, Aug 31, 2004, in forum: Java
    Replies:
    9
    Views:
    379
    John C. Bollinger
    Sep 2, 2004
  4. Replies:
    3
    Views:
    353
    Eric Sosman
    Nov 16, 2007
  5. wx

    a synchronization problem on webservice

    wx, Aug 8, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    82
Loading...

Share This Page