473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Synchronization problem

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;

}

}

}
Nov 12 '08 #1
2 1205
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;

}

}

}
Nov 12 '08 #2
Hi,

Thanks for the bad news :)

/ Johan

"bruce barker" <br*********@discussions.microsoft.comwrote in message
news:E4**********************************@microsof t.com...
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;

}

}

}

Nov 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan \(Pickles\) Sklan-Willis | last post by:
Hi All, I have built 2 VB6-ACCESS XP, programs (ADODC component used). The software works fine, updates the database and all, without any troubles. I however now need to do something else with...
0
by: Efim | last post by:
Hi, I have got some problem with sending of events in .NET. I am using remouting. The client has got 2 objects for receiving different types of events (responses and events) The server has got...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
7
by: Robert | last post by:
Hi, I have noticed some synchronization issues when using javascript. I'll give you an example. It is easy to reproduce the problem if you can cause some delay in the webserver before sending...
0
by: catchrohith | last post by:
hi all i hav to develop a flash with voice using .NET, .Generation of Flash agents with voice is possible using .NET. The only problem is that lip synchronization of the talking character...
12
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
1
by: khashkarara | last post by:
I am in a great problem. I need to understand cleaarly about Database Synchronization and Centralization For my thesis work. For this purpose i need some detailed documents on Database...
0
by: satees | last post by:
Hi, I am creating website with using webservices and oracle database. In which i have expect the synchronization problem in future. So how to solve or prevent this synchronization problem through...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.