简体   繁体   中英

.NET return response before async task completes

I have a webapp, and users make requests to change large amounts of data. Maybe persisting 1000 or more objects.

I validate these requests up front, and then begin persisting the data. It really doesn't take LONG to persist individually, but multiple by a few thousand and it does. I'm using an OR/M framework and don't want to move away from it... and it is not really the bottleneck.

I could build persist a smaller object graph - simply a 'plan' on how to persist the rest of it and then persist it some other way, but I'd really prefer an easier solution.

Ideally, I'd validate, then start an async task that would persist all of the data. The user would get confirmation after it is validated and they can go along on their merry little way.

Is this possible in .NET? Any suggestions?

Thanks

You can validate the input first, set some sort of status label indicating that everything went fine, and then launch the persistence part in a new Thread.

I mean something like this:

 protected void Button1_Click(object sender, EventArgs e)
 {
      new Thread(() => { 
          //logic to save the data goes here.
      }).Start();
      Label1.Text = " Input validated. Thanks!";
 }

The user can safely close the browser and the operation will finish.

EDIT

Broken Glass' comment is correct in the sense that this approach does not scale well. There's a better way to perform this type of async operations. It's detailed here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM