473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_OnS tart() 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 1258
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.p yesmeadow.com> wrote in message
news:di******** ************@ne wsfep2-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_OnS tart() 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.p yesmeadow.com> wrote in message
news:di******** ************@ne wsfep2-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_OnS tart() 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 oversimplificat ion:

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.p yesmeadow.com> wrote in message
news:lW******** ************@ne wsfep2-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
3895
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 and Application_OnEnd (see below.) The ShoreTelMakeCallTransfer object is a simple VB test object which
2
3576
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 there is an event that you can watch for whenever an exception is thrown? If this isnt possible can anyone suggest the best way to handle unforeseen exceptions? I'm assuming that surrounding huge chunks of application code in try catch blocks would be...
6
2019
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 with new codebehinds for each page I want to have translated. But my question is - is it ok to keep the same codebehind file for a certain page which is translated into different languages. In this way I only need to change to aspx-file (html) of...
0
2483
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 and handle them at the same time - in parallel. The funny thing is that the behaviour of the implementation I created depends on the existence of Global.asax in the Web application. Here is the source of the page that handles the regular GET...
0
1558
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 living. What if the solution was just to help each other, simply by giving and receiving. This would be a radical global experiment in faith. Imagine people all around the world connecting instantaneously and just giving and receiving money to each...
8
1899
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
2338
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. -------------------------------------------------------------------------------- Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack...
4
1757
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 diagnostics; use sigtrap;
3
12784
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 target of the thread and I dont know how exactly return would work in such a case). I am sure I am missing something very fundamental here. The essential pieces of my code that cause the problem would be something like this:...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10090
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
9173
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...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.