Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Synchronization problem

Reply
Thread Tools

Synchronization problem

 
 
Johan Íhrn
Guest
Posts: n/a
 
      11-12-2008
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;

}

}

}


 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      11-12-2008
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;
>
> }
>
> }
>
> }
>
>
>

 
Reply With Quote
 
 
 
 
Johan Íhrn
Guest
Posts: n/a
 
      11-14-2008
Hi,

Thanks for the bad news

/ Johan

"bruce barker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
>>
>> }
>>
>> }
>>
>> }
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
synchronization: concurrent method access problem Piper707@hotmail.com Java 3 11-16-2007 06:20 PM
xvid sound synchronization problem danko Computer Support 2 05-25-2007 02:46 PM
Thread synchronization problem Vincent Lascaux Java 9 09-02-2004 07:53 PM
Multiplt clock synchronization problem Francis VHDL 1 04-27-2004 08:34 AM
Synchronization Problem Sami Lakka Java 1 12-04-2003 10:23 AM



Advertisments