I am new to Windows Phone Development. I want to make a HTTP request to REST(GET) service from my code. The result will be in Json.
I want get my result Asynchronously and while retriving I want to show Progress Ring.
Help me solving this. Thanks in Advance.
I tried following code. But getting exception
System.Exception was unhandled by user code HResult=-2147417842 Message=The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) Source=Windows StackTrace: at Windows.UI.Xaml.Controls.ProgressRing.put_IsActive(Boolean value) at eBooks.MainPage.GetResultCallBack(IAsyncResult result) at MS.Internal.Modern.ClientHttpWebRequest.c__DisplayClass1e.b__1c(Object state2) InnerException:
public void MakeRequest(string requestUrl)
{
try
{
SearchProgress.IsActive = true;
SearchButton.IsEnabled = false;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl) as HttpWebRequest;
request.BeginGetResponse(GetResultCallBack, request);
}
catch (Exception)
{
}
}
private void GetResultCallBack(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request!=null)
{
try
{
WebResponse response = request.EndGetResponse(result);
if (response!=null)
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(BookSearch));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
ebook = objResponse as BookSearch;
}
}
catch (Exception)
{
throw;
}
finally
{
SearchProgress.IsActive = false;
SearchButton.IsEnabled = true;
DataLoaded(ebook);
}
}
}
The exception describes what the problem is. You are trying to access SearchProgress control which is a ProgressRing created on a UI thread from a non-UI thread. In order to fix this, you need to make the change from the UI thread, and for that you need the use the CoreWindow dispatcher:
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Debug.WriteLine("I'm on UI thread");
SearchProgress.IsActive = false;
SearchButton.IsEnabled = true;
}
);
User contributions licensed under CC BY-SA 3.0