假装在Silverlight WP7同步调用 [英] Faking synchronous calls in Silverlight WP7

查看:106
本文介绍了假装在Silverlight WP7同步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从完整的.NET框架的版本WP7移植一些code和我遇到的问题与同步VS异步调用。

I'm porting some code from the full .NET framework to the WP7 version and I'm running into an issue with synchronous vs async calls.

 string response;
 string requestString = GenerateReqString();
 HttpWebRequest req = (HttpWebRequest) WebRequest.Create("endpoint");
 req.Method = "POST";
 req.ContentType = "text/xml";

 req.ContentLength = requestString.Length;

 StreamWriter sw = new StreamWriter (req.GetRequestStream(), System.Text.Encoding.ASCII);
 sw.Write(requestString);
 sw.Close();

 StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream());
 response = sr.ReadToEnd();
 sr.Close();

响应字符串然后解析成由该方法返回的对象的列表。

The response string is then parsed into a list of objects that is returned by the method.

我遇到的问题是,有没有一种方法,使在Silverlight / WP7同步呼叫。如果我用回电话,我会得到不同的功能的响应,将无法从原来的函数返回。有没有办法要么使调用同步或回调函数回返回到拉开帷幕异步调用方法?

The problem I'm having is that there isn't a way to make the call synchronously in Silverlight/WP7. If I use a call back I'll get the response in a different function and wont be able to return it from the original function. Is there a way to either make the call synchronously or return from the CallBack function back to the method that kicked off the async call?

推荐答案

您需要以不同的方式思考问题。为了使异步事情的感觉同步,做到这一点最简单的方法是调整你的code到利用的延续传递风格

You need to think about the problem differently. To make asynchronous things "feel" synchronous, the easiest way to do it is to restructure your code to make use of 'continuation passing style'.

在本质上,而不是调用返回一个值,然后你处理该值的功能,可以调用的函数,传递一个匿名函数作为一个委托给它。然后调用的函数将调用委托,传递的字符串中。

In essence, instead of calling a function that returns a value and then you process that value, you call a function, passing an anonymous function as a delegate to it. The called function will then invoke the delegate, passing in the string.

下面是一个例子,它使用匿名函数和lambda表达式:

Here is an example, which uses anonymous functions and lambdas:

void DoSomethingAsync( Action<string> callback ) {
    HttpWebRequest req; // TODO: build your request

    req.BeginGetResponse( result => {
        // This anonymous function is a closure and has access 
        // to the containing (or enclosing) function.
        var response = req.EndGetResponse( result );

        // Get the result string and call the callback
        string resultString = null; // TODO: read from the stream

        callback(resultString);
    }, null );
}

这是二分之一的溶液。下一部分,是实际调用此。想象一下,你有一个ICommand实例或简单,在需要调用此函数和获得字符串按钮单击事件。而不是让字符串调用此函数,并提供一个回调方法(这将是一个闭合)。

This is one half the solution. The next part, is to actually call this. Imagine you have an ICommand instance or simpler, a button click event that needed to call this function and "get the string". Instead of "getting the string" you call this function and supply a callback method (which will be a closure).

void btnGo_Click( object sender, EventArgs e ) {
    DoSomethingAsync( resultString => {
        // This anonymous function is called when the web request has
        // finished and has your string. 

        // Now that we have the string, we can go and process it.
        ProcessWebResponseResult( resultString );
    });
}

下面是一个真正的好文章进一步解释的概念:
http://blogs.msdn.com/b/wesdyer/archive/2007/12/22/continuation-passing-style.aspx

Here is a really good article explaining the concept further: http://blogs.msdn.com/b/wesdyer/archive/2007/12/22/continuation-passing-style.aspx

这篇关于假装在Silverlight WP7同步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆