简体   繁体   中英

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

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.

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.

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).

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