/// <summary> /// Synchronous Wrapper method around FetchAsynch() method /// </summary> /// <param name="cacheIndex"></param> /// <param name="cacheIndexItem"></param> public NetworkResponse Fetch(string uri, string filename, Dictionary <string, string> headers) { PostNetworkResponse = new NetworkResponse(); FetchParameters fetchParameters = new FetchParameters() { Uri = uri, Headers = headers, FileName = filename }; DateTime dtMetric = DateTime.UtcNow; // set callback and error handler OnDownloadComplete += new NetworkingEventHandler(FetcherAsynch_OnDownloadComplete); OnError += new NetworkingErrorHandler(FetcherAsynch_OnError); System.Threading.ThreadPool.QueueUserWorkItem(parameters => { try { FetchAsynch(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 ;) } }, fetchParameters); // 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)) { string message = "FetcherAsynch call to FetchAsynch timed out. uri " + fetchParameters.Uri; MXDevice.Log.Error(message); MXDevice.Log.Debug(string.Format("FetchAsynch timed out: Uri: {0} Time: {1} milliseconds ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds)); NetworkResponse networkResponse = new NetworkResponse() { Message = message, URI = fetchParameters.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("FetchAsynch Completed: Uri: {0} Time: {1} milliseconds Size: {2} ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds, (PostNetworkResponse.ResponseBytes != null ? PostNetworkResponse.ResponseBytes.Length : -1))); return(PostNetworkResponse); }
/// <summary> /// Synchronous Wrapper method around FetchAsynch() method /// </summary> /// <param name="cacheIndex"></param> /// <param name="cacheIndexItem"></param> public NetworkResponse Fetch( string uri, string filename, Dictionary<string, string> headers ) { PostNetworkResponse = new NetworkResponse(); FetchParameters fetchParameters = new FetchParameters() { Uri = uri, Headers = headers, FileName = filename }; DateTime dtMetric = DateTime.UtcNow; // set callback and error handler OnDownloadComplete += new NetworkingEventHandler( FetcherAsynch_OnDownloadComplete ); OnError += new NetworkingErrorHandler( FetcherAsynch_OnError ); System.Threading.ThreadPool.QueueUserWorkItem( parameters => { try { FetchAsynch( 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 ;) } }, fetchParameters ); // 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 ) ) { string message = "FetcherAsynch call to FetchAsynch timed out. uri " + fetchParameters.Uri; MXDevice.Log.Error( message ); MXDevice.Log.Debug( string.Format( "FetchAsynch timed out: Uri: {0} Time: {1} milliseconds ", uri, DateTime.UtcNow.Subtract( dtMetric ).TotalMilliseconds ) ); NetworkResponse networkResponse = new NetworkResponse() { Message = message, URI = fetchParameters.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( "FetchAsynch Completed: Uri: {0} Time: {1} milliseconds Size: {2} ", uri, DateTime.UtcNow.Subtract( dtMetric ).TotalMilliseconds, ( PostNetworkResponse.ResponseBytes != null ? PostNetworkResponse.ResponseBytes.Length : -1 ) ) ); return PostNetworkResponse; }