Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX and System.InvalidOperationException

Status
Not open for further replies.

vbkris

Programmer
Jan 20, 2003
5,994
IN
Hi Guys,

The info is as follows:
I have a web page that does a very heavy operation. In order to make it a bit more interactive i implemented AJAX on that page (have not used the inbuilt AJAX controls).

The page psuedo code is as follows:

In Page_Load
------------
If CallBy is AJAX then 'I idenfity this with a particular Request.Form item
Return Back Session("RecordCount")
response.end
exit sub
end

'Non AJAX call. This happens only for the first call made to the page
Create a new Thread for a sub called UpdateData()

ClientScript.RegisterStartupScript the JavaScript that will enable AJAX invocation

In UpdateData()
---------------
for loop for around more than 100000 records
Session("RecordCount")=CurrentRecordCount
'Do a heavy operation on each record.
next


The AJAX request will be made every 1 second.

The code is working fine on my system. However when i deploy this to a colleague's system he gets the following error:
System.InvalidOperationException: Collection was modified after the enumerator was instantiated

This error comes once in maybe 20 AJAX requests (the thread continues to execute thereby updating the record count and therefore the next AJAX request gives the correct data count). I was able to capture this error from the AJAX response which i set to a <DIV> tag.

The worst part is that this error is being thrown ONLY at the application level (I identofied this by setting a debug point in the App_error event of Global.asax). I am unable to capture this error at the page level (In the Catch block of Page_Load)!

I also tried playing around with Trace and was able to idenfity one strage aspect with the failing AJAX request.

The session collection appearing for the failed request is virtually empty (even though there are around 10 session values for every user).

I also tried delaying the AJAX request to 5 seconds. No go there.

I also tried using SynchLock commands. This also did not lead me anywhere.

Finally doubting that this could be an issue with locking of the session variable i commented the Session accessing block of AJAX in the page_load.

If CallBy is AJAX then
'Return Back Session("RecordCount")
response.end()
exit sub
end

The error is still being thrown.

One more conclusion that i was able to make is that this error is NOT being thrown by the block executed by the thread code as the next AJAX request gives me the correct value.

My Page_Load event does NOT have any loops on any collections whatsoever.

Currently i am in the process of writing a simple AJAX request and testing the result. But please do let me know if any of you have come across such a strange error.


Known is handfull, Unknown is worldfull
 
Hi,

An update from my side. i have zeroed in on the line that is causing this error.

for loop for around more than 100000 records
Session("RecordCount")=CurrentRecordCount
'Do a heavy operation on each record.
next

If i comment the Session setting line it does not throw the error.

Now i am on the lookout on different ways to query data from a spawned thread...

Known is handfull, Unknown is worldfull
 
Processing on 100,000 records is going to take a lot of resources no matter what you do to it. Whilst it's not a brilliant solution, if it's definitely the line that you mentioned that is causing the problem, how about you just update it every x records e.g.
Code:
If CurrentRecordCount Mod 10 = 0 Then Session("RecordCount")= CurrentRecordCount



-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
thanks mate, i tried that too. i did it for once in 100 records. it still fails :(

I just need a way to get some kind of process status back from the thread to the users.

Infact before this option i tried to do this the ASP way (Response.Writes and Response.Flushes). that proves to be too heavy (as lots of info gets sent to the browser).

The only other option was threading. Any other ways in which this can be achieved???

Known is handfull, Unknown is worldfull
 
how can i do that?

The session is used to communicate with the thread that is being spawned. The spawned thread will not be accessible to the AJAX request (as the request that spawned it would have ended).

and this is how i am returning it:
response.write(Session("RecordCount"))

so it goes bacj to AJAX only as a string...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top