0

有没有人有一个基于 JQuery 的客户端在 SignalR Hub 上调用基于异步任务的方法的工作示例?有关服务器端异步任务的示例,请参阅 SignalR Doco 中的以下代码。

public Task<int> AsyncWork() 
    {
        return Task.Factory.StartNew(() => 
        {
            // Don't do this in the real world
            Thread.Sleep(500);
            return 10;
        });
    }
4

1 回答 1

2

这是服务器端的示例 * : *

public void  AsyncWork() 
    {

          // start working in a new thread
          var task = Task.Factory.StartNew(() => dosomething());

    task.ContinueWith(t =>
                                  {
                                      Caller.notifyResult(t.Result);
                          });

    }


   private int dosomething()
        {
            int result =0;
            return result;
        }

在客户端:

<script type="text/javascript">
// init hub 
var proxy = $.connection.signals;

// declare response handler functions, the server calls these
proxy.notifyResult = function (result) {
    alert("the result was: " + result);
};

// start the connection
$.connection.hub.start();

// Function for the client to call
function AsyncWork() {
    proxy.AsyncWork(); 
}
</script>
于 2013-05-10T03:11:16.783 回答