473,799 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threads with ASP.NET

Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but I
do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Object2) .
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will only
have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
application (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2 thread
safe(Using Lock for example), or ASP.NET will take care of different
sessions(users) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it creates a
separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks

Nov 18 '05 #1
8 1365
You get the wrong concept this is asp.net not windowsform.

chanmm

"Angel" <an*****@hotmai l.com> wrote in message
news:uO******** ******@TK2MSFTN GP12.phx.gbl...
Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but I
do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Object2) .
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will only have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
application (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2 thread
safe(Using Lock for example), or ASP.NET will take care of different
sessions(users) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it creates a separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks

Nov 18 '05 #2
I presume you are using a singleton pattern, I always lock when writing and
depending on the object type lock when reading aswell.

remember, ASP.NET is stateless, yes you can create a object that sits around
but ASP.NET could kill it when it gets recycled and you don't get any
warning of this, so if you are saving things in memory that you need to keep
then watch out.

Steve

"Angel" <an*****@hotmai l.com> wrote in message
news:uO******** ******@TK2MSFTN GP12.phx.gbl...
Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but I
do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Object2) .
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will only have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
application (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2 thread
safe(Using Lock for example), or ASP.NET will take care of different
sessions(users) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it creates a separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks

Nov 18 '05 #3
Thanks Steve,
You got it right. I use something as singleton. I have one object per
class.
Let say my user is Student
When user login I create a UserObject that I save in Session["User"].
Then on demand I fill its collection properties student classes ,
teachers.... Any of those collections have a class reference to a
DataPersistObje ct instance.
Every class of those collection properties has a class reference to an
object (DataPersistObj ect ) which has only 2 methods
Load(obj, id) and Save(obj), they retrieve or save one object per time from
a database to collection and vice versa.
The problem is that I do not know how different sessions will interact
(behave) when accessing the methods of this object.
Should I synchronize "Load" and "Save" methods using "lock(this) " within
the methods body , or ASP.Net will take care of this concurrent access to
this methods between different sessions.

When the user logout all this will be released since the only place keeping
reference to this properties is UserObject, which is released when
session["User is released"].

My guess is that class references to my DataPersist object would not be
released until Application is running, because they have been created with
Static constructor like this:

static Teachers()
{
//fdmobj is defined in the base class
fdmobj = new DMTeacher();

}
Angel

"Steve Drake" <Steve@_NOSPAM_ .Drakey.co.uk> wrote in message
news:u$******** *****@TK2MSFTNG P09.phx.gbl...
I presume you are using a singleton pattern, I always lock when writing and depending on the object type lock when reading aswell.

remember, ASP.NET is stateless, yes you can create a object that sits around but ASP.NET could kill it when it gets recycled and you don't get any
warning of this, so if you are saving things in memory that you need to keep then watch out.

Steve

"Angel" <an*****@hotmai l.com> wrote in message
news:uO******** ******@TK2MSFTN GP12.phx.gbl...
Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but I do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Object2) .
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will only
have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
application (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2 thread safe(Using Lock for example), or ASP.NET will take care of different
sessions(users) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it

creates a
separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks


Nov 18 '05 #4
Hi Angel:

If the Load and Save methods are stateless, then you don't need to
worry about locking. A stateless method will use only local variables
and parameters to the method. You should consider making the methods
static, then all of your object1 instances won't even need to store a
reference to object2.

You are correct in thinking ASP.NET will use multiple threads to
service incoming requests. It won't prevent thread unsafe code from
bad bahvior.

--
Scott
http://www.OdeToCode.com

On Tue, 10 Aug 2004 10:48:46 -0400, "Angel" <an*****@hotmai l.com>
wrote:
Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but I
do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Object2 ).
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will only
have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
application (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2 thread
safe(Using Lock for example), or ASP.NET will take care of different
sessions(users ) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it creates a
separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks


Nov 18 '05 #5
I was thinking of doing this methods static and now when you mentioned it I
think that this is better idea anyway.
I did not made them static originally because I've had an idea to keep some
values there.
Later I realized that this will give me only troubles and give it up.
Thanks
Angel
Nov 18 '05 #6
Yeah...

With the archetecture of ASP.NET, I imagine that the only way to
preserve an object across many sessions is by creating an actual windows
service that preserves the state of the object. If this were the case,
then you would just have a queue of items waiting on this service, which
would negate any cross-access issues. Of course, it's probably way
easier to just use static functions, like stated somewhere else in this
thread.

Mike
Angel wrote:
Thanks Steve,
You got it right. I use something as singleton. I have one object per
class.
Let say my user is Student
When user login I create a UserObject that I save in Session["User"].
Then on demand I fill its collection properties student classes ,
teachers.... Any of those collections have a class reference to a
DataPersistObje ct instance.
Every class of those collection properties has a class reference to an
object (DataPersistObj ect ) which has only 2 methods
Load(obj, id) and Save(obj), they retrieve or save one object per time from
a database to collection and vice versa.
The problem is that I do not know how different sessions will interact
(behave) when accessing the methods of this object.
Should I synchronize "Load" and "Save" methods using "lock(this) " within
the methods body , or ASP.Net will take care of this concurrent access to
this methods between different sessions.

When the user logout all this will be released since the only place keeping
reference to this properties is UserObject, which is released when
session["User is released"].

My guess is that class references to my DataPersist object would not be
released until Application is running, because they have been created with
Static constructor like this:

static Teachers()
{
//fdmobj is defined in the base class
fdmobj = new DMTeacher();

}
Angel

"Steve Drake" <Steve@_NOSPAM_ .Drakey.co.uk> wrote in message
news:u$******** *****@TK2MSFTNG P09.phx.gbl...
I presume you are using a singleton pattern, I always lock when writing


and
depending on the object type lock when reading aswell.

remember, ASP.NET is stateless, yes you can create a object that sits


around
but ASP.NET could kill it when it gets recycled and you don't get any
warning of this, so if you are saving things in memory that you need to


keep
then watch out.

Steve

"Angel" <an*****@hotmai l.com> wrote in message
news:uO****** ********@TK2MSF TNGP12.phx.gbl. ..
Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but
I
do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Objec t2).
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will


only
have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
applicatio n (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2
thread
safe(Using Lock for example), or ASP.NET will take care of different
sessions(use rs) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it


creates
a
separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks



Nov 18 '05 #7
> Of course, it's probably way
easier to just use static functions, like stated somewhere else in this
thread.


True, except if one tries to utilize objects polymorphic behavior.
Static methods cannot be virtual.

Nov 18 '05 #8
> With the archetecture of ASP.NET, I imagine that the only way to preserve
an object across many sessions is by creating an actual windows service
that preserves the state of the object.
no, it's not the only way. and your approach isn't recommended either since
a windows service is not guaranteed to be running when your application
runs. the cache object shares state across sessions, also the static
modifier implements one copy per appdomain which is shared across sessions.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"Mike Newton" <MN*****@discus sions.microsoft .com> wrote in message
news:Ox******** ******@TK2MSFTN GP12.phx.gbl... Yeah...

With the archetecture of ASP.NET, I imagine that the only way to preserve
an object across many sessions is by creating an actual windows service
that preserves the state of the object. If this were the case, then you
would just have a queue of items waiting on this service, which would
negate any cross-access issues. Of course, it's probably way easier to
just use static functions, like stated somewhere else in this thread.

Mike
Angel wrote:
Thanks Steve,
You got it right. I use something as singleton. I have one object per
class.
Let say my user is Student
When user login I create a UserObject that I save in Session["User"].
Then on demand I fill its collection properties student classes ,
teachers.... Any of those collections have a class reference to a
DataPersistObje ct instance.
Every class of those collection properties has a class reference to an
object (DataPersistObj ect ) which has only 2 methods
Load(obj, id) and Save(obj), they retrieve or save one object per time
from
a database to collection and vice versa.
The problem is that I do not know how different sessions will interact
(behave) when accessing the methods of this object.
Should I synchronize "Load" and "Save" methods using "lock(this) " within
the methods body , or ASP.Net will take care of this concurrent access to
this methods between different sessions.

When the user logout all this will be released since the only place
keeping
reference to this properties is UserObject, which is released when
session["User is released"].

My guess is that class references to my DataPersist object would not be
released until Application is running, because they have been created
with
Static constructor like this:

static Teachers()
{
//fdmobj is defined in the base class
fdmobj = new DMTeacher();

}
Angel

"Steve Drake" <Steve@_NOSPAM_ .Drakey.co.uk> wrote in message
news:u$******** *****@TK2MSFTNG P09.phx.gbl...
I presume you are using a singleton pattern, I always lock when writing


and
depending on the object type lock when reading aswell.

remember, ASP.NET is stateless, yes you can create a object that sits


around
but ASP.NET could kill it when it gets recycled and you don't get any
warning of this, so if you are saving things in memory that you need to


keep
then watch out.

Steve

"Angel" <an*****@hotmai l.com> wrote in message
news:uO***** *********@TK2MS FTNGP12.phx.gbl ...

Hi everybody,
I am new to ASP.NET, and my question might be obvious to most of you but


I
do not seem to find many things about threads and ASP.NET.

I have an object(Object 1) which need some service from another
object(Obje ct2).
Object 2 has only two methods and no members.

Load(Object1 obj, int id);
Save(Object1 obj);

I will have thousands instances from Object1, but to save memory I will

only

have one instance of Object2.
I decided to create only one reference of Object2 and keep it as class
member of Object1 class.

Now if I have only one instance per class, any user logged to the
applicati on (any session) will use the same Object2 obj2 reference.
My question is: Should I make Load() and Save() methods of Object2


thread
safe(Usin g Lock for example), or ASP.NET will take care of different
sessions(us ers) accessing this methods.

Again I do not create separate threads. I just wonder if ASP.NET make a
separate thread for any of application users (any session) or it


creates
a

separate process(with separate allocated memory ) per session and this
process memory area is guarded from other processes.

Thanks



Nov 18 '05 #9

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

Similar topics

3
5404
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import thread, sys
0
2012
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux distributions using the Native Posix Threading Layer (NPTL), this isn't entirely true anymore and is AFAIK unsupported (using a pid to signal/identify threads). I removed it from my config.sh script and ran Configure -der. make test runs just fine. ...
6
3206
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
34
10818
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
3
22178
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been assigned a name. As in .net there is no way we can get the managed threads, I am thinking of making a win32 call and check their status.
10
1684
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
6
2528
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the data is collected is that we have different schedules (so one set of data is collected say every second, another set of data might be collected every 30 seconds, and so on).
3
5974
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
10
1765
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that T2, T4 and T5 are already running. Thanks, Darian
4
2260
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should start five threads and process five files at a time. Is this a good idea? I picked the number five at random... I was thinking that I might check the number of processors and start a multiple of that, but then I remembered KISS and it seemed that...
0
10490
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
10259
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...
1
10238
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
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
9077
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
6809
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
5467
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...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.