Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 22nd, 2004, 02:56 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default Multiple simultaneous asynchronous requests

Hi everyone,
I was reading an article in Imar's Homepage
http://imar.spaanjaars.com/ViewConte...=4&CATEGORY=11
about
Howto Create a Hit Counter Using a Text File in ASP.NET
now I have a question in Session_Start event we make everytime a file
and set it to new counters ,here I cant get if there are many simultaneous asynchronous requests what will happen
Code:
protected void Session_Start(Object sender, EventArgs e)
{

  totalNumberOfUsers += 1;
  currentNumberOfUsers += 1;

  string counterFile = Server.MapPath("/Counter.txt"); 
  if (File.Exists(counterFile))
  {
    File.Delete(counterFile);
  }
  StreamWriter sw = File.CreateText(counterFile);
  sw.WriteLine ("Total\t" + totalNumberOfUsers);
  sw.Close(); 

}
I dont know maybe Session_Start could guarantee this and dont let simultaneous requests at the same time(while the file is ready for writing)
can anybody guide me in this regard.
(I know also there is such situations in Database but the DBMS can handle it correctly but here I dont know how it could be handled).
Thanks.

--------------------------------------------
Mehdi.:)
__________________
_____________
<font color=\"teal\"><font size=\"1\"><b>Mehdi.
software student.</b></font id=\"size1\"></font id=\"teal\">
  #2 (permalink)  
Old August 22nd, 2004, 05:34 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

You'll get into problems when there are two users starting a session at exactly the same time as indeed the file will be locked by the first users when the second tries to write to it.

That's why I am saying this at the end of the article:
Quote:
quote:A disadvantage of using text files is that it's difficult to scale out your Web site to lots of users. Only one user has access to the file at any given moment, so if you have a real busy site, you'll soon run into problems.
Take a look at the following article to see how you can improve the counter page even further by using a database instead of a text file.
Howto Create a Hit Counter Using a Database in ASP.NET
Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #3 (permalink)  
Old August 22nd, 2004, 06:19 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hmmmmm,right
now if I want to work with files correctly what I should do?
that means if I want to change this example how I should change?
(I think I should use BeginRead,.. but I dont know how?can you help me change this example how it works fine for multi users,I have to not use databases)
Thanks in advance.

--------------------------------------------
Mehdi.:)
  #4 (permalink)  
Old August 22nd, 2004, 06:37 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

sorry,another question!
does this method(files) work correctly at all?,I think if the file is locked for the first user and then becom unlocked for the second simultaneous user,it should work correctly ,am I right?
with thanks.

--------------------------------------------
Mehdi.:)
  #5 (permalink)  
Old August 22nd, 2004, 06:38 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

I don't think much can be done about this. Since Session_Start fires without any user interaction, you cannot ask them to "hold" or try again.

What you could do is not write to the file directly, but queue up the information in Application state, for example. Then when you've queued 20 users, write them to file at once. That way, only for every 20 (just an example) users do you have to write to the file.

What's wrong with using a database?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #6 (permalink)  
Old August 22nd, 2004, 06:39 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Yes. As soon as you call sw.Close();, the file becomes available for other users.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #7 (permalink)  
Old August 22nd, 2004, 07:37 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hmmmm,yes,so sw.close could solve this problem and assure us the result is right.
Imar I found an article in MSDN about Asynchronous File I/O here
http://msdn.microsoft.com/library/de...nousFileIO.asp
I cant get it(especialy it's example its too long),I dont have any problem with databases but I want to learn working with files too so I dont want to work with databases...
Imar can you help me change this example and work with BeginRead,I think I should make a file everytime asynchronously and then work with BeginWrite instead of WriteLine.
can you help me do it easily...
(I dont want to work with Application state because you know it consumes lots of resources especially for queuing 20 users meanwhile its not clear, maybe we need queuing 200 users)
so I want to work only with files(because I want to learn it I dont want to try other possible(maybe better!) ways,
sorry I waste your time.

--------------------------------------------
Mehdi.:)
  #8 (permalink)  
Old August 22nd, 2004, 07:39 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

again sorry, another question!,
when you declare two static global fields in Global.asax is there any difference between such variables and Application Items?
(because Global class derives System.Web.HttpApplication although the type of Application object is HttpApplicationState)
why you didnt use application Items instead of two static global variables?
can you make me understand again?
Thanks again.


--------------------------------------------
Mehdi.:)
  #9 (permalink)  
Old August 22nd, 2004, 08:20 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

BeginWrite and BeginRead are used for asynchronous writing and reading within a method / application. So your main thread can continue working, while a separate thread takes care of IO stuff.

In the hitcounter scenario this is not going to help you at all. You still have the issue of two users writing at the same time, each from within a separate thread. Maybe the problem can even get worse, when the second user writes a different value to the file than the first.
Conclusion: you cannot use this file based method on a busy site; you'll always run into problem when two users try to write to the file at the same time.
I understand you're trying to learn working with files, but you picked the wrong topic to try it out with.

Theoretically, you could also use Application state for the values. However, I chose static variables as it is a concept that easily translates to other (.NET) applications.

Using application state is no problem, even with 100 or 1000 or 100,000 users for the hit counter. All you are saving is the value of the hitcounter, and the number of users you have queued. So, all you'd be saving in Application state are two ints. That shouldn't affect performance at all.

Does this clarify things?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #10 (permalink)  
Old August 22nd, 2004, 09:17 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yes,Thank you
but I dont want to be based on Application state,maybe we have 1000 users in our queue and suddenly IIS is restarted,so we will lose it(by knowing we cant save it to files everytime!)
I think as you mentioned I have to use datbase,
but really I want to simulate something like MS Access while working on files but(not all MS Access!!!) I dont know where I should start.I mean how I should handle multiple simultaneous asynchronous requests on a unique file(in the other word how I can handle a file while there are many requests at the same time for accessing and changing this file in my application)
Thanks before.

--------------------------------------------
Mehdi.:)
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Concurrent Requests to XmlHttpRequest deepak.vasudevan Ajax 3 March 22nd, 2006 03:56 AM
Multiple Concurrent Requests to XmlHttpRequest deepak.vasudevan Javascript 0 November 14th, 2005 05:49 AM
Multiple http-requests are stuck MereMortal C++ Programming 2 May 9th, 2005 03:46 PM
Simultaneous multiple file download balamuralivenkat Classic ASP Basics 6 December 17th, 2003 10:37 PM
How can I do simultaneous scrolling of 2 wnd? Sonin Nickolay Visual C++ 1 August 21st, 2003 04:34 AM



All times are GMT -4. The time now is 01:15 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.