Abhijeet
Abhijeet

Reputation: 13856

Simulate Fire & Forget scenario using C# Threading

I have a web service, which takes lot of time to execute. I am planning to delegate the processing task to a background thread & return the acknowledgement response to the user immediately.

In this case I was worried about the life-time of the background thread.
Will background thread complete the delegated task, before main method/thread finishes execution?

Upvotes: 1

Views: 328

Answers (2)

Brian Gideon
Brian Gideon

Reputation: 48949

You should call your service asynchronously.

var service = new YouServiceClient();
service.SomeMethodCompleted +=
  (sender, args) +=
  {
    //  Put code here to handle the response and extract a return value or whatever.
  }
service.SomeMethodAsync();

Refer to How to: Call WCF Service Operations Asynchronously for more details.

Upvotes: 0

NeddySpaghetti
NeddySpaghetti

Reputation: 13495

Looks like your background thread might get aborted if the app pool gets recycled. Have a look at this article ThreadPool.QueueUserWorkItem in Web Service for “Fire and Forget” task

Upvotes: 0

Related Questions