473,406 Members | 2,954 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.

sharing a global object - threading issues

wh
I really just need some reassurance that I'm doing the right thing really.
Here goes...

I have an object which needs to be available to all sessions. This is being
created in the Application_OnStart() event. Once created the object is
stored in the Application collection.

The idea of this global object is to manage resources (xml files) between
different sessions/users.

For example user #1 may require write access to file1.xml. If user #2
requests write access to the same file then the request will be denied. The
global object will have a method such as IsPageInUse() which will check
whether the requested page is already in use (I intend to store this
information in a collection) and return true/false accordingly.

What I'd like to know is how much support would I need to include in the
IsPageInUse() method to handle calls from multiple pages? I assume that
multiple calls can be made into the function (as opposed to one at a time
like with STA COM objects) and I will therefore need to add code to ensure
there are no race conditions when checking whether pages are in use, etc.

Thank you,
Wayne.
Nov 18 '05 #1
4 1244
I don't know anything about your IsPageInUse method, so I can't answer that
specific question. However, you should be storing your class in the
Application Cache if you want full threading support.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

"wh" <wa***@nospam.pyesmeadow.com> wrote in message
news:di********************@newsfep2-win.server.ntli.net...
I really just need some reassurance that I'm doing the right thing really.
Here goes...

I have an object which needs to be available to all sessions. This is being created in the Application_OnStart() event. Once created the object is
stored in the Application collection.

The idea of this global object is to manage resources (xml files) between
different sessions/users.

For example user #1 may require write access to file1.xml. If user #2
requests write access to the same file then the request will be denied. The global object will have a method such as IsPageInUse() which will check
whether the requested page is already in use (I intend to store this
information in a collection) and return true/false accordingly.

What I'd like to know is how much support would I need to include in the
IsPageInUse() method to handle calls from multiple pages? I assume that
multiple calls can be made into the function (as opposed to one at a time
like with STA COM objects) and I will therefore need to add code to ensure
there are no race conditions when checking whether pages are in use, etc.

Thank you,
Wayne.

Nov 18 '05 #2
"wh" <wa***@nospam.pyesmeadow.com> wrote in message
news:di********************@newsfep2-win.server.ntli.net...
I really just need some reassurance that I'm doing the right thing really.
Here goes...

I have an object which needs to be available to all sessions. This is being created in the Application_OnStart() event. Once created the object is
stored in the Application collection.

The idea of this global object is to manage resources (xml files) between
different sessions/users.

For example user #1 may require write access to file1.xml. If user #2
requests write access to the same file then the request will be denied. The global object will have a method such as IsPageInUse() which will check
whether the requested page is already in use (I intend to store this
information in a collection) and return true/false accordingly.

What I'd like to know is how much support would I need to include in the
IsPageInUse() method to handle calls from multiple pages? I assume that
multiple calls can be made into the function (as opposed to one at a time
like with STA COM objects) and I will therefore need to add code to ensure
there are no race conditions when checking whether pages are in use, etc.


You're right. You have to handle the case of multiple pages touching your
object at the same time. This can be made easier by being careful to
encapsulate all accesses to your object so that it all goes through methods
and properties of your object. In particular, you must be very careful about
exposing any public fields in your object, as there is no way to control
access to them. Instead, make them public properties and control access to
them in the property setter. Gross oversimplification:

private int _counter;
public int Counter
{
get {return _counter;}
set
{
lock (this)
{
_counter = value;
}
}
}
Note that even this isn't adequate, as it doesn't save you from problems
with "object.Counter++".

If you haven't done multi-threaded programming before, let me offer a bit of
advice: Murphy rules here. Any race condition you haven't prevented from
happening _will_ happen. This is especially true when you haven't tested
under heavy load on a fast, multi-CPU system. And don't assume that because
it hasn't broken visibly, that the code is correct. It just means that you
weren't paying attention when it broke, or that it hasn't broken yet.
--
John
Nov 18 '05 #3
wh
I will essentially have a collection storing the names of all resources in
use. A method will allow a resource to be flagged as 'in use' which will
effectivly add the name of the resource to the collection. The IsPageInUse()
method (I guess this should be called IsResourceInUse() ) will simply look
to see if the name of the resource is held in the collection.

What I basically need to do is ensure that the collection I'm using to store
the names of resources is locked whenever I need to make changes to it. This
will ensure that if a user requests that 'file2.xml' be locked then should
another user request the same resource the collection will be locked at the
appropriate times thus eliminating the race condition.

I'd also be grateful if you can elaborate more on the Application cache as
opposed to the Application object which is what I'm using now. What
advantages are there?

Thank you,
Wayne.
Nov 18 '05 #4
You can use the Synclock() statement in VB.Net, or the lock() statement in
C# to lock any object which you need to. Just pass the object to the
Statement, and put any statements that you want executed when the lock is
established inside the block.

For more detailed information about Application State, see the following SDK
article:\

http://msdn.microsoft.com/library/de...ationstate.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
"wh" <wa***@nospam.pyesmeadow.com> wrote in message
news:lW********************@newsfep2-win.server.ntli.net...
I will essentially have a collection storing the names of all resources in
use. A method will allow a resource to be flagged as 'in use' which will
effectivly add the name of the resource to the collection. The IsPageInUse() method (I guess this should be called IsResourceInUse() ) will simply look
to see if the name of the resource is held in the collection.

What I basically need to do is ensure that the collection I'm using to store the names of resources is locked whenever I need to make changes to it. This will ensure that if a user requests that 'file2.xml' be locked then should
another user request the same resource the collection will be locked at the appropriate times thus eliminating the race condition.

I'd also be grateful if you can elaborate more on the Application cache as
opposed to the Application object which is what I'm using now. What
advantages are there?

Thank you,
Wayne.

Nov 18 '05 #5

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

Similar topics

5
by: Grant Schenck | last post by:
Hello, I posted an this issue before but I'm clearer on what is going on. I have a simple web site with a global.asa and a single .asp file. In global.asa I handle both Application_OnStart...
2
by: thechaosengine | last post by:
Hi everyone, Is there anyway to create some sort of catch-all in windows forms applications that could ensure that no unexpected exceptions bring down an application? For example, perhaps...
6
by: Thomas Andersson | last post by:
Hi all, I've been thinking of about adding new languages to our website and is currently investigating how this could be done. And I know that one way to go would be to create new aspx-pages...
0
by: hynek.cihlar | last post by:
A strange behaviour thatI found in ASP.NET 2.0. I am trying to issue a callback request (handled by ICallbackEventHandler and RaiseCallbackEvent) and a regular GET request in the client browser...
0
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a...
8
by: ecir.hana | last post by:
Dear list, maybe I'm overlooking something obvious or this is not possible at all or I don't know. Please, consider the following code: ## insert here anything you like def changer():
2
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
4
by: pcaisse | last post by:
I'm having issues sharing global variables with Explorer. This problem probably has a simple answer (as with most newbie questions). The script.pl file: #!/usr/bin/perl -w use strict; use...
3
by: dedalusenator | last post by:
Hello Folks, My first posting here and I am a stuck in figuring out the exact way to update a global variable from within a function that doesnt return any value (because the function is a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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.