开发者

Redirecting Page whilst still executing slow Linq.DataContext.SubmitChanges() in ASP.NET

开发者 https://www.devze.com 2023-03-01 17:26 出处:网络
For an Intranet application I have a web page save function that can lead to substantial action on several database tables. I am using Linq2SQL and some 20 seconds of browser idle get swallowed up by

For an Intranet application I have a web page save function that can lead to substantial action on several database tables. I am using Linq2SQL and some 20 seconds of browser idle get swallowed up by 3 seperate Linq.DataContext.SubmitChanges() calls. These calls occur right at the end of the code called by the SaveButton postback.

I want the actual work needed to accumulate these database changes to be done within the save button action. However, given no problems or conflicts detected, I would then like to redirect the user to another page immediately after this (so they don't have to stare at a screen doing something they cannot stop. Then as the user co开发者_如何学运维ntinues I would like to call all the SubmitChanges().

Ideal workflow:

void btnSaveClick() 
{
    CalculateChangeSetProcedure
    Redirect somewhere...
    SubmitChangesProcedure
}

So my problem is Response.Redirect(..., true) will just shut things down. Is there any way I can do this with ASP.NET procedures or will I have to do it async on another thread?


If you are trying to submit a small amount of data then the idle time you have is not normal. First thing you can do is check the submit function and see there is anything you can optimize. For example, if you are submitting 50 rows and you call SubmitChanges after each of them, this could slow down the things significantly. The solution in this case is to prepare all the date and submit it at once.

Another option is calling a service that submits the changes for you. In this way you the saving will be done asynchronously and would not interrupt the user. After the service is done you can get some simple jQuery popup or something to inform the user that the saving is completed if this is important.


Start a seperate background thread for SubmitChanges() call before doing the redirection.

e.g.

void btnSaveClick() 
{    
      CalculateChangeSetProcedure
      ThreadPool.QueueUserWorkItem(new WaitCallback(this.SubmitTheStuff), null);
      Redirect somewhere..
}
private void SubmitTheStuff(object obj)
{
      SubmitChangesProcedure
}    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号