473,785 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Queu eUserWorkItem(n ew WaitCallback(th is.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.Ite ms 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.Proprie tary.Assembly.O utOfMyControl

{

public class SomeWorkToBeDon e

{

public void DoWork()

{

ThreadPool.Queu eUserWorkItem(n ew WaitCallback(th is.WorkThread)) ;

}

private void WorkThread(obje ct obj)

{

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

ArrayList items = helper.GetItems ();

int unknownNumberOf TimesToGetItems = new Random().Next() ;

for (int i = 0; i < unknownNumberOf TimesToGetItems ; i++)

{

if (helper.GetItem s().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.Cu rrent != null)

{

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

if (entries == null)

{

HttpContext.Cur rent.Items[GetHashCode()] = entries = new
ArrayList(BackE ndStore.GetItem s());

}

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.Ge tItems();

}

}

}

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 unknownNumberOf Items = new Random().Next() ;

for (int i = 0; i < unknownNumberOf Items; i++)

list.Add(1);

return list;

}

}

}
Nov 12 '08 #1
2 1228
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.Queu eUserWorkItem(n ew WaitCallback(th is.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.Ite ms 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.Proprie tary.Assembly.O utOfMyControl

{

public class SomeWorkToBeDon e

{

public void DoWork()

{

ThreadPool.Queu eUserWorkItem(n ew WaitCallback(th is.WorkThread)) ;

}

private void WorkThread(obje ct obj)

{

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

ArrayList items = helper.GetItems ();

int unknownNumberOf TimesToGetItems = new Random().Next() ;

for (int i = 0; i < unknownNumberOf TimesToGetItems ; i++)

{

if (helper.GetItem s().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.Cu rrent != null)

{

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

if (entries == null)

{

HttpContext.Cur rent.Items[GetHashCode()] = entries = new
ArrayList(BackE ndStore.GetItem s());

}

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.Ge tItems();

}

}

}

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 unknownNumberOf Items = new Random().Next() ;

for (int i = 0; i < unknownNumberOf Items; i++)

list.Add(1);

return list;

}

}

}
Nov 12 '08 #2
Hi,

Thanks for the bad news :)

/ Johan

"bruce barker" <br*********@di scussions.micro soft.comwrote in message
news:E4******** *************** ***********@mic rosoft.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.Que ueUserWorkItem( new WaitCallback(th is.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.Ite ms 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.Proprie tary.Assembly.O utOfMyControl

{

public class SomeWorkToBeDon e

{

public void DoWork()

{

ThreadPool.Queu eUserWorkItem(n ew
WaitCallback(t his.WorkThread) );

}

private void WorkThread(obje ct obj)

{

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

ArrayList items = helper.GetItems ();

int unknownNumberOf TimesToGetItems = new Random().Next() ;

for (int i = 0; i < unknownNumberOf TimesToGetItems ; i++)

{

if (helper.GetItem s().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.Cu rrent != null)

{

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

if (entries == null)

{

HttpContext.Cur rent.Items[GetHashCode()] = entries =
new
ArrayList(Back EndStore.GetIte ms());

}

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.Ge tItems();

}

}

}

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 unknownNumberOf Items = new Random().Next() ;

for (int i = 0; i < unknownNumberOf Items; 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
1716
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 this. The 2 programs are identical, they are built for 2 offices 200 miles apart, and I want the programs to synchronize the data between the 2, automatically. The 2 computers do not have static IP's, they however do have internet access via...
0
1421
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 two objects for sending of these events. The client opens tcp port 0 to receive events: if (ChannelServices.GetChannel("tcp") == null) {
5
15584
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. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
4
3203
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 work across multiple processes just the one. I was wondering if any one new which one used the least overhead. Im at current using mutexes but was wondering if there was something a bit
7
5404
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 the page. //filename = page.html <script type="text/javascript"> function test1()
0
950
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 wont work, plz help me if u hav enough information about how can i acheive lip synchronization? The code demo for generation of flash agents with .NET is on http://www.codeproject.com/useritems/TTS.asp
12
2015
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 that they all wait until they've all done one job before starting off on each of their next jobs. I have been thinking about this for a day and can't seem to find a solution.
1
1358
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 Synchronization and Centralization. If anybody help me it would be a great chance to complete my thesis work and i would be very greatfull to him. Please contact with my email address. My email address is: email address removed - moderator
0
1001
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 coding or any way. Any idea that would be great ! Thanks, Satees
0
1981
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 a LOT of synchronization overhead. After a bit of digging I concluded that this synchronization has to do with the access to a global locale inside the stream. The problem can be seen by running the small distilled benchmark code
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.