c# - How bad is this pattern? -


I am using this method when I need to control various async operations. I mean, not specifically for WebRequest (I know that you can set the timeout property), I used it as an example of a pattern.

  var request = WebRequest.Create (someUri); WebResponse response = null; Request.BeginGetResponse (Results => {asyncRequest = (WebRequest) Result. Asyncunch; Response = asyncRequest.EndGetResponse (Results);}, Requests); Datetime Timeout = DateTime.Now Edecand (10); While (feedback == faucet & amp; amp; time-time.Now & lt; = timeout) {thread. Sleep (0); } If (feedback == empty) throw a new exception ("timeout!");  

Anywhere I read about threads. Sleep (), I have heard that there is a left thing to do this, but I do not think this use is misused in the case.

I know that it is possible that it can be slightly more than 10 seconds, but this is not important to me.

So, it's a bad way

Edit: Maybe I should make clear what I'm trying to do.

The objective is to control the maximum time spent waiting for the call. I know that it dashes the purpose of an async call, but the intention was never asynchronous, I am using it as a means of controlling only when I quit a call.

Waiting for wait times method support outside time, use it. Something like this:

  var asyncResult = request.BeginGetResponse (... asyncResult.AsyncWaitHandle.WaitOne (Timespace.fresaconds (10))  

Comments