Permalink
Browse files

Merge SL and non-SL sections. Add HandleCallbackonUIThread option - e…

…nabled by default.
  • Loading branch information...
mj1856 committed Feb 11, 2012
1 parent b5a86e5 commit fb78557d410376b3dc4f5808c6450780d90a9508
@@ -69,12 +69,38 @@ public RequestState()
public Action<TResponse, Exception> OnError;
+#if SILVERLIGHT
+ public bool HandleCallbackOnUIThread { get; set; }
+#endif
+
+ public void HandleSuccess(TResponse response)
+ {
+ if (this.OnSuccess == null)
+ return;
+
+#if SILVERLIGHT
+ if (this.HandleCallbackOnUIThread)
+ System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => this.OnSuccess(response));
+ else
+ this.OnSuccess(response);
+#else
+ this.OnSuccess(response);
+#endif
+ }
+
public void HandleError(TResponse response, Exception ex)
{
- if (OnError != null)
- {
- OnError(response, ex);
- }
+ if (this.OnError == null)
+ return;
+
+#if SILVERLIGHT
+ if (this.HandleCallbackOnUIThread)
+ System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => this.OnError(response, ex));
+ else
+ this.OnError(response, ex);
+#else
+ OnError(response, ex);
+#endif
}
public void StartTimer(TimeSpan timeOut)
@@ -122,6 +148,10 @@ public void SetCredentials(string userName, string password)
public StreamDeserializerDelegate StreamDeserializer { get; set; }
+#if SILVERLIGHT
+ public bool HandleCallbackOnUIThread { get; set; }
+#endif
+
public void SendAsync<TResponse>(string httpMethod, string absoluteUrl, object request,
Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
@@ -160,6 +190,9 @@ public void SetCredentials(string userName, string password)
Request = request,
OnSuccess = onSuccess,
OnError = onError,
+#if SILVERLIGHT
+ HandleCallbackOnUIThread = HandleCallbackOnUIThread,
+#endif
};
requestState.StartTimer(this.Timeout.GetValueOrDefault(DefaultTimeout));
@@ -301,10 +334,7 @@ private void ReadCallBack<T>(IAsyncResult asyncResult)
response = (T)this.StreamDeserializer(typeof(T), reader);
}
- if (requestState.OnSuccess != null)
- {
- requestState.OnSuccess(response);
- }
+ requestState.HandleSuccess(response);
}
catch (Exception ex)
{
@@ -381,4 +411,5 @@ private void HandleResponseError<TResponse>(Exception exception, RequestState<TR
public void Dispose() { }
}
+
}
Oops, something went wrong.

0 comments on commit fb78557

Please sign in to comment.