, , , , , service.LoginCompleted
public static Task<int> SignIn( string username, string password )
{
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
EventHandler<WebService.LoginCompletedEventArgs> onLoginCompleted = null;
onLoginCompleted = ( object sender, WebService.LoginCompletedEventArgs e ) =>
{
service.LoginCompleted += onLoginCompleted;
if(e.Error != null)
{
tcs.SetResult( -1 );
}
else
{
tcs.SetResult( (int)e.Result );
}
};
service.LoginCompleted += onLoginCompleted;
service.LoginAsync( username, password );
return tcs.Task;
}
, ,
public static Task<int> SignIn( string username, string password )
{
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
EventHandler<WebService.LoginCompletedEventArgs> onLoginCompleted = ( object sender, WebService.LoginCompletedEventArgs e ) =>
{
if(e.Error != null)
{
tcs.SetResult( -1 );
}
else
{
tcs.SetResult( (int)e.Result );
}
};
service.LoginCompleted += onLoginCompleted;
tcs.Task.ContinueWith(task => service.LoginCompleted -= onLoginCompleted);
service.LoginAsync( username, password );
return tcs.Task;
}
try/catch tcs.Task .
, service.LoginAsync( username, password ) ,
...
try
{
service.LoginAsync( username, password );
}
catch(SomeParticularException ex)
{
tcs.SetException(ex);
}
return tcs.Task;