473,324 Members | 2,248 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,324 software developers and data experts.

handle a long running process nicely

The Human Tangent
I have a very long running server side process that imports items from an XML file. It needs to run in a simple web page. Importing 3 items takes about 25 seconds due to a very slow running API process and not much can be done about that. In total the process needs to import about 1800 items, so there is no way this can run before the page can complete without timing out. Also I need to give some kind of simple visual progress to the user (importing 37 of 1997 etc.)

I've put the process to import as a stand alone file that takes a query string (x= current row position ; y = total rows ; f = filename saved on server )

Originally I thought that the first request could call the second request etc.

So processXml.aspx?x=1&y=1997&f=myfile.xml when completed would reload processXml.aspx?x=2&y=1997&f=myfile.xml then processXml.aspx?x=3&y=1997&f=myfile.xml etc

however if i use
Expand|Select|Wrap|Line Numbers
  1. string fileName = Request.QueryString["f"].ToString();
  2. string tX = Request.QueryString["x"].ToString();
  3. string tY = Request.QueryString["y"].ToString();
  4. int iX = Convert.ToInt16(tX);
  5. string sX2 = Convert.ToString(iX+1);
  6.  
  7. ///do a long running thing
  8. ...
  9. Response.Redirect("processXml.aspx?x=" + sX2 + "&y=" + tY + "&f=" + fileName, true);
  10.  
[sX2 = x+1]

no output is returned to the user

then I thought what about

Expand|Select|Wrap|Line Numbers
  1. Response.AppendHeader("Refresh", "0; processXml.aspx?x=" + sX2 + "&y=" + tY + "&f=" + fileName);
  2.  
but this always loads the same page with the same query string, no ideas why, but X does not progress, it stays in a loop where x always equals 1, on page load after the reload x always equals 1, and sX2 always equals 2 but when reloaded x still equals 1

So....

what is a good approach to this?

I'm wondering about using an IFrame maybe and calling it in JavaScript but this would be new territory for me, there must be a good pattern for this sort of thing

Any help very gratefully received :)
Jan 26 '10 #1
3 3726
Frinavale
9,735 Expert Mod 8TB
It's pretty hard to achieve this because of the disconnected nature of the web environment that you're working with....

Think about what generally happens:
  • the web browser makes a request to the server for a page
  • the web server passes the request to your code and your code generates the page
  • the page is sent to the browser
  • the page is rendered by the browser

Now, your process is going to take a long time....so this means that the page cannot be sent to the browser until it's finished processing.

I have tried getting around this a few times but have run into a wall every time. What I've tried doing is preforming an Ajax request to the server to start the process and then have an Ajax Timer control postback to the server to see how far along the process is.

You can try to do this but I have a feeling that you're going to run into the same problem I did while trying to solve this problem.

I don't know how to get around the road block that I've run into. If you like to see how far I've gotten check out post #15 in this thread: ASP.NET Progress

If you figure out how to get around my problem please let me know!

Happy coding,
-Frinny
Jan 26 '10 #2
In the end the answer was very simple.

I've had a great read through the thread you suggested and some other suggested from there and so on, was greatly amused by the closed thread that had the helpful comment "JUST USE AJAX" yes in proper shouty-case :)

It's a great site this one and I am sure I'll keep coming back, thank you for the friendly helpful welcome.

In the end the answer was a very simple bit of javascript.

I broke the process into a single page that does one part and then calls the next page. Once the process is complete and if x<y (ie we're not on the last one) then I write a timed window relocate, but after everything else.

eg:

Expand|Select|Wrap|Line Numbers
  1.                 Response.Write("<h2>Completed " + tX + " of " + tY + "</h2>");
  2.  
  3.                 string sNextX = "processXml.aspx?x=" + sX2 + "&y=" + tY + "&f=" + fileName.Replace("\\", "\\\\");
  4.  
  5.                 StringBuilder sbScript = new StringBuilder();
  6.                 sbScript.AppendLine("<script type=\"text/javascript\">");
  7.                 sbScript.AppendLine("setTimeout(\"GoNextX()\", 2000);");
  8.                 sbScript.AppendLine("function GoNextX()");
  9.                 sbScript.AppendLine("{");
  10.                 sbScript.Append("window.location = \"");
  11.                 sbScript.Append(sNextX);
  12.                 sbScript.AppendLine("\"; ");
  13.                 sbScript.AppendLine("}");
  14.                 sbScript.AppendLine("</script>");
  15.  
  16.                 ClientScript.RegisterStartupScript(typeof(Page), "nextX", sbScript.ToString());
  17.  
so at the top the address to the next call is processed - with a lot of quadruple slashing going on for the benefit of javascript and c#

after the process has completed the redirect is written to the browser and it processes the next one, but crucially the current page terminates in memory and flushes all its output to the page

this now runs as a 3hour+ request absolutely fine :)
Jan 27 '10 #3
Frinavale
9,735 Expert Mod 8TB
I don't think I fully understand your application needs.

If you preform a Response.Redirect() then the web browser is told by the server to redirect to another web page. I'm glad that you were able to get this to work for your needs...but I'm a little disappointed that my question still remains.

I've been thinking about it in my spare time (on lunch, while driving, etc) and you have given me a new idea on how to approach this problem.

I'm thinking that maybe I could use Server.Transfer to shift the request off to to be processed by another ASPX page, thus freeing the original page up for accepting new requests. (Recall that Server.Transfer sends all of the information that has been assembled for processing by one .asp file to a second .asp file...this means that I could gather all of the information necessary to do the processing in the original page and send all of that information to the next page which would do the process). I think that this might work! It seems sound in theory, but so does handling asynchronous requests to the same ASPX page asynchronously so I'm not 100% sure.

-Frinny
Jan 27 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Phil B Brubaker | last post by:
I'm trying to create my own shellexecute program named shell4OD and am having problems with handles. Here is my code ... ===== Private Declare Function ShellExecute Lib _ "shell32.dll" Alias...
2
by: Gary Homewood | last post by:
I have an ASP page calling a stored procedure. I have set the connection and command timeouts and I have error checking so I can trap a timeout from the stored proc (as detailed here)...
11
by: objectref | last post by:
Hi to all, is there a way to get the window handle of the main window of an application or process ? For example, if someone opens Microsoft Word, he gets a window so he/she can write text....
5
by: PontiMax | last post by:
Hi, when I press the OK button of my dialog box a long-running task is initiated. Therefore I would like to make visible a div section right after clicking the button where a user-friendly...
1
by: Patrick Dugan | last post by:
Is it possible to get the handle of a running service? I have a program (ActiveX program) running in memory. When I start my service I need to pass the service's handle to that program in order...
39
by: tydbowl | last post by:
I have a problem where the below code chunk causes handle leaks on some machines. The leak APPEARS to be handles to the registry key: HKCU\Software\Microsoft\Windows\CurrentVersion\Internet...
1
by: Anonieko | last post by:
Query: How to display progress bar for long running page Answer: Yet another solution. REFERENCE: http://www.eggheadcafe.com/articles/20050108.asp My only regret is that when click the...
14
by: lmttag | last post by:
Hello. We're developing an ASP.NET 2.0 (C#) application and we're trying to AJAX-enable it. We're having problem with a page not showing the page while a long-running process is executing. So,...
4
by: commander_coder | last post by:
Hello, I write a lot of CGI scripts, in Python of course. Now I need to convert some to long-running processes. I'm having trouble finding resources about the best practices to do that. ...
32
by: John Wright | last post by:
I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.