public NetworkResponse PostBytes( string uri, byte[] postBytes, string contentType, string verb, Dictionary<string, string> headers ) { PostNetworkResponse = new NetworkResponse(); RequestParameters requestParameters = new RequestParameters() { PostBytes = postBytes, Uri = uri, Headers = headers, ContentType = contentType, Verb = verb }; DateTime dtMetric = DateTime.UtcNow; // set callback and error handler OnComplete += new PostRequestEventHandler( PostRequest_OnComplete ); OnError += new PostRequestErrorHandler( PostRequest_OnError ); System.Threading.ThreadPool.QueueUserWorkItem( parameters => { try { RequestAsynch( parameters ); } //catch ( Exception e ) //{ // // You could react or save the exception to an 'outside' variable // // threadExc = e; //} finally { autoEvent.Set(); // if you're firing and not forgetting ;) } }, requestParameters ); // WaitOne returns true if autoEvent were signaled (i.e. process completed before timeout expired) // WaitOne returns false it the timeout expired before the process completed. if ( !autoEvent.WaitOne( DefaultTimeout ) ) { //throw TimeoutException( "Waited ages for a thread to come in." ); string message = "PosterAsynch call to RequestAsynch timed out. uri " + requestParameters.Uri; MXDevice.Log.Error( message ); MXDevice.Log.Debug( string.Format( "PosterAsynch timed out: Uri: {0} Time: {1} milliseconds ", uri, DateTime.UtcNow.Subtract( dtMetric ).TotalMilliseconds ) ); NetworkResponse networkResponse = new NetworkResponse() { Message = message, URI = requestParameters.Uri, StatusCode = HttpStatusCode.RequestTimeout, ResponseString = string.Empty, Expiration = DateTime.MinValue.ToUniversalTime(), }; MXDevice.PostNetworkResponse( networkResponse ); return networkResponse; } //if ( threadExc != null ) //{ // throw threadExc; //} MXDevice.Log.Debug( string.Format( "PosterAsynch Completed: Uri: {0} Time: {1} milliseconds Size: {2} ", uri, DateTime.UtcNow.Subtract( dtMetric ).TotalMilliseconds, ( PostNetworkResponse.ResponseBytes != null ? PostNetworkResponse.ResponseBytes.Length : -1 ) ) ); return PostNetworkResponse; }
public NetworkResponse PostBytes(string uri, byte[] postBytes, string contentType, string verb, Dictionary <string, string> headers) { PostNetworkResponse = new NetworkResponse(); RequestParameters requestParameters = new RequestParameters() { PostBytes = postBytes, Uri = uri, Headers = headers, ContentType = contentType, Verb = verb }; DateTime dtMetric = DateTime.UtcNow; // set callback and error handler OnComplete += new PostRequestEventHandler(PostRequest_OnComplete); OnError += new PostRequestErrorHandler(PostRequest_OnError); System.Threading.ThreadPool.QueueUserWorkItem(parameters => { try { RequestAsynch(parameters); } //catch ( Exception e ) //{ // // You could react or save the exception to an 'outside' variable // // threadExc = e; //} finally { autoEvent.Set(); // if you're firing and not forgetting ;) } }, requestParameters); // WaitOne returns true if autoEvent were signaled (i.e. process completed before timeout expired) // WaitOne returns false it the timeout expired before the process completed. if (!autoEvent.WaitOne(DefaultTimeout)) { //throw TimeoutException( "Waited ages for a thread to come in." ); string message = "PosterAsynch call to RequestAsynch timed out. uri " + requestParameters.Uri; MXDevice.Log.Error(message); MXDevice.Log.Debug(string.Format("PosterAsynch timed out: Uri: {0} Time: {1} milliseconds ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds)); NetworkResponse networkResponse = new NetworkResponse() { Message = message, URI = requestParameters.Uri, StatusCode = HttpStatusCode.RequestTimeout, ResponseString = string.Empty, Expiration = DateTime.MinValue.ToUniversalTime(), }; MXDevice.PostNetworkResponse(networkResponse); return(networkResponse); } //if ( threadExc != null ) //{ // throw threadExc; //} MXDevice.Log.Debug(string.Format("PosterAsynch Completed: Uri: {0} Time: {1} milliseconds Size: {2} ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds, (PostNetworkResponse.ResponseBytes != null ? PostNetworkResponse.ResponseBytes.Length : -1))); return(PostNetworkResponse); }