我正在开发一个WPF聊天应用程序,有两个项目
在制作login request
时,我希望用户只有在服务器已经启动且无法用户登录时才能连接。
我在此应用程序中使用SignalR
。如何在登录前检查服务器是否已经启动,然后让用户连接到它。
使用
可以在JavaScript中实现$.connection.hub.start().done(function ()
{
});
如何在wpf应用程序中实现此目的?
答案 0 :(得分:-1)
您可以通过以下代码之类的处理来处理它:
try
{
var hubConnection = new HubConnection(url,...);
... ; /*defining proxies and handlers*/
hubConnection.Start().Wait();
}
catch(Exception ex)
{
/*handle exception code */
}
或者:注意
hubconnection.Start()
会返回Task
,因此如果您想运行代码并检查代码是否成功运行或存在异常,您可以使用ContinueWith
方法:
var hubConnection = new HubConnection(url,...);
... ; /*defining proxies and handlers*/
hubConnection.Start().ContinueWith( t=> {
/* code to run if connection has been made successfully */
},TaskContinuationOptions.OnlyOnRanToCompletion );
所以我们可以说这个带有TaskContinuationOptions.OnlyOnRanToCompletion
的ContinueWith相当于$ .connection.hub.start()。done()