Search code examples
c#multithreadingamazon-web-serviceswebmethod

c# asp multi threading or web method


I'm after a bit of advice concerning multi threading / web services, and if / how I should use one of these for my issue.

I have a button on my c# asp site that does a bunch of api calls, and the process can take several minutes. I want to split this process off, so that it can perform 'in the background' and will not upset the user experience. The process is tighly wound into my current project, so it isn't easy to split code off into a separate web service project.

So..

  1. If I run the following Thread code, will it perform successfully if the user kills the browser? What are my issues/considerations?

Thread thread = new Thread(new ThreadStart(RefreshData));

  1. If the above doesnt work, can I make this work by adding some [web methods] to the same project, and call them from the same project? (if so - any tips?)

  2. If I do this will it successfully perform them in another iis thread? ie.. if the users kills their browser - can I be sure that the processes from the service will complete successfully?

Also, I'm planning to host the site on AWS - so if there are considerations from that perspective any insights would also be helpful.

Thanks in advance gang..


Solution

  • You can run background threads. They run even if the visitor closed the browser window. You can associate these threads/workload with the visitor using sessions/cookies, so even after they re-visit the website they can pick up with the progress.

    What you have to pay attention is to keep the web server process from recycling. That would occur after a certain timeout or lifetime by default. You must change relevant settings.

    Also, keep a permanent reference to the thread you created to prevent it garbage collected.