如何在 C# wp7 silverlight 中实现我自己的异步回调? [英] How can I implement my own async callback in C# wp7 silverlight?

查看:18
本文介绍了如何在 C# wp7 silverlight 中实现我自己的异步回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将引擎及其单元测试移植到 C# WP7 silverlight 中.因为它使用 http 请求,所以它需要按照框架的规定进行异步.我正在移植的引擎的 Android 版本在工作线程上使用同步阻塞套接字.只要整个操作完成(不仅仅是 http 请求),它就会调用回调.

I am trying to port an engine into C# WP7 silverlight, along with its unit tests. Because it uses http requests, it needs to be asynchronous as dictated by the framework. The Android version of the engine I am porting uses synchronous blocking sockets on a worker thread. It invokes a callback whenever the entire operation is complete (not just the http request).

问题 - 如何包装回调机制,使其执行可在 [异步] 单元测试中使用的异步回调?

Question - how can I wrap the callback mechanism so that it does an asynch callback that can be used in an [Asynchronous] unit test?

我希望它做这样的事情:

I want it to do something like this:

using System;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;

[TestClass]
public class WebRequestsTests : WorkItemTest
{
    [TestMethod, Asynchronous]
    public void TestWebRequest()
    {
        var webRequest = WebRequest.CreateHttp("http://www.stackoverflow.com");

        webRequest.BeginGetResponse(result =>
        {
            EnqueueCallback(() =>
            {
                WebResponse response = webRequest.EndGetResponse(result);

                // process response 

                TestComplete(); // async test complete 
            });
        }, null);
    }
} 

我是否需要实现 IAsync 接口或类似的东西?

Do I need to implement the IAsync interface or something similar?

我想让它做这样的事情:

I'd like to have it do something like this:

using System;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;

[TestClass]
public class WebRequestsTests : WorkItemTest
{
    [TestMethod, Asynchronous]
    public void TestWebRequest()
    {
        MyThread thread = new MyThread();

        thread.Start(result =>
        {
            EnqueueCallback(() =>
            {
                WebResponse response = thread.EndGetResult(result);

                // process response 

                TestComplete(); // async test complete 
            });
        }, null);
    }
} 

我也不确定我对 lambda 表达式是否也 100% 满意,所以如果删除它们,那就更好了!;)

Not sure I'm also 100% comfortable with the lambda expressions either, so if those are removed, even better! ;)

谢谢

推荐答案

您无需创建新线程来处理 EndRequest 或 EndResponse 回调 - 这些将在 ThreadPool 的后台线程上为您调用.所以像你的第一个代码示例应该可以工作.

You don't need to create a new thread to deal with the EndRequest or EndResponse callback - these will be called for you on a background thread from the ThreadPool. So something like your first code example should work.

如果您不喜欢嵌套的 lambda,只需声明命名方法 :).您可以在可以在结果对象中检索的 Begin... 方法中传递状态信息.

If you don't like the nested lambdas just declare named methods :). You can pass state information in the Begin... methods which you can retrieve in the result object.

你问的有点奇怪 - 你用同步版本包装一个异步框架,并用异步版本重新包装那个.听起来您正在为自己创造额外的工作,以保持对您的端口的忠诚.您还将使用一些额外的内存来保持一个额外的线程不做任何事情(堆栈至少为 1MB).

What you're asking is kind of weird - you're wrapping an async framework with sync version, and rewrapping that with an async version. It sounds like you're creating extra work for yourself in order to stay faithful to your port. You will also use some extra memory to keep an extra thread alive doing nothing (1MB for the stack at least).

如果您仍然想这样做,请查看此链接 不过.

If you still want to do it check out this link though.

这篇关于如何在 C# wp7 silverlight 中实现我自己的异步回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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