robert
robert

Reputation: 1533

Thread Sleep in c#

Ok. I'm calling an external script [Edit: web service] that's doing an asynchronous task. It usually takes one to two minutes to complete.

In the meantime i want to display to the user the "please wait" message, since i need to make another call to check if the previous task has been completed yet before i continue.

Using timers is not exactly a good solution. I need the user to actually wait before i continue.

So the question i have is whether or not thread.sleep will put the entire web application to sleep or just the one for the current UI?

I don't want the UI for other website visitors to hang. I'm not sure how this works in the production environment.

I use iis 7 on windows 2008 r2

Thanks

Upvotes: 0

Views: 965

Answers (3)

user2599849
user2599849

Reputation:

You shouldn't use Thread.Sleep. Assuming this is a web application...

One request should start the task and return a job token.

With another request, the user should be able to check the status of that job (by passing the token).

When the job is complete, the user should be able to get the result of the job (by passing the token).

Upvotes: 1

Maximc
Maximc

Reputation: 1762

More info would be nice. However:

I guess you are going to make a website (asp.net mvc or webforms) since you are using iis7. Just run the server and let the client use a Ajax call to ask status of the task. The task when finished should be stored somewhere, preferably a database. You can also use xml or whatever to have a point where you can then save the taskid and or it is completed or not. And just let the ajax do a call every 5 sec until it returned at the taskid true on completed.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500525

Thread.Sleep will only affect the current thread - but it's not what you want to do anyway.

You should return a complete response from your web application - but one which starts a Javascript timer to fire in a few seconds, and then make an AJAX call back to your web app to check whether the task has completed. (You should include some sort of "task ID" in your response so that the server knows which task to check.)

If you just sleep before returning the response, your user won't see any message until the sleeping has completed.

Upvotes: 4

Related Questions