1

I have an ASP.NET page that requires data from another DLL and the process might take a long time. So, I set out to use APM. But when I try that, the page just never stops loading. It loads indefinitely. Is there something I'm doing wrong?

Here is my web page:

List<string> allVoices;
GetAllVoicesDelegate getVoicesDelegate;
internal delegate List<String> GetAllVoicesDelegate();

protected void Page_Load(object sender, EventArgs e)
{
  Page.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginGetDropDownValues),
       new EndEventHandler(EndGetDropDownValues));
}

public IASyncResult BeginGetDropDownValues(object o,EventArgs args,AsyncCallback cb,object obj)
{
   getVoicesDelegate = MyLib.getStrings;
   return getVoicesDelegate.BeginInvoke(EndGetDropDownValues,null);
}

public void EndGetDropDownValues(IASyncResult ar)
{
   allVoices = getVoicesDelegate.EndInvoke(ar);
}

protected override OnPreRenderComplete(EventArgs e)
{
  if(allVoices.Count>0)
  {
    foreach(String str in allVoices)
    {
       Response.Write(str);
    }
  }
  base.OnPreRenderComplete(e);

}

Here is the MyLib.getStrings() method in another DLL:

public List<String> getStrings()
{
   List<String> allStr=new List<string>();
   allStr.Add("1");
   allStr.Add("2");
   allStr.Add("3");
   allStr.Add("4");
}

4 Answers 4

1

If you have to get data from a long running process, making async calls from the web page won't help you because in the end, the process needs to finish before the page can finish rendering. Making async calls frees you to do other stuff in the mean time, but the page can't render until all the activity on it is finished.

I think you'll have to take a different approach, either using Ajax to poll the server until the response is ready, or creating an intermediate page that tells the user to wait until the process is complete. Once it's complete, the page refreshes and the user sees the data.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not overly familiar with your approach to asynchronous computing in asp.net (I generally use ajax to make requests to web services, etc.)

One possible flaw, from my point of view is the OnPreRenderComplete handler. This handler is not asynchronous to the best of my knowledge. It is used to handle the event of when all page controls are loading and ready.

The OnPreRenderComplete method is called when the prerendering stage of the page life cycle is complete. At this stage of the page life cycle, all controls are created and the page is ready to render the output.

In this event handler, you are making reference to allVoices class-level member which is processed by the async handlers. Is it possible then the code in this handler has to wait for the async delegate to complete? (in essence defeating the purpose of async).

Again just a guess. What happens if you take out the `OnPreRenderComplete' handler? or comment out the code within?

Additionally - I think the article you referenced is a little outdated and may not apply to web application development? Generally asynchronous programming on the web is done via AJAX and/or Web services/WCF?

2 Comments

thx 4 ur answer,kevin. after taking out the OnPreRenderComplete handler, i still get the same problem.
Sorry that didn't help you. I the other answers are leading you in the right direction. The code you're basing the solution off of is meant for Winforms I think. You'll need to look at AJAX and or webservices to do async web application programming.
0

Most likely your problem is your ASP.NET page compiling (typically takes seconds), and not the dll loading (typically takes only a few milliseconds).

Comments

0

The code sample you have provided launches some work on a separate thread and allows the asp.net worker process to continue processing your page request without blocking on your long running task. If the long running task takes a long time to complete, then your page will take a long time to render.

Start by replacing the begin with some kind of mocked up code to return an invoice. If your page completes almost immediately, then your async wiring is correct and you probably need to move to some kind of polling approach which uses the first request to start the task and renders a "we are processing your request" page to the user. This page then uses ajax to poll the system for some kind of completion flag, once that is received, the user is redirected to the result page and the results are displayed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.