2014-03-19 78 views
0

我想調用一個嵌套的異步函數,但我沒有得到所需的數據。 因爲我在Silverlight中使用wcf服務,所以我只能使用異步函數。Silverlight中嵌套的異步函數

在我的代碼中,我保存了一組包含userdata的行。在我保存之前,我需要檢查用戶名是否唯一。現在我只需要找出第一個,然後退出循環,並顯示一條消息,user.for簡單起見,我已經全部剝離額外數據的功能,這是它的外觀

 private void SaveUsers(bool CloseForm) 
      { 
       ObservableCollection<User> _UpdatedUsers = new ObservableCollection<User>(); 
       DatabaseServiceLocal _dataService = new DatabaseServiceLocal(Database); 

       foreach (UserViewModel _User in _AllUsers) 
       { 
        //bool success = _dataService.IsUserNameUnique(_User.UserName, _User.UserID, Database.CurrentClient.ClientID); 
        if (_User.Dirty && !_User.IsBlank) 
        {      
         _dataService.CheckIsUserNameUnique += (s, e) => 
         { 
          if (e.IsUnique) 
           _UpdatedUsers.Add(_User.SaveAsUser()); 
          else 
           { 
           _UpdatedUsers = new ObservableCollection<User>(); 
           csaMessageBox.Show(string.Format("Username {0} is not allowed as it already exists in the system. Please choose a different username.", ""), null); 
           return; 
           } 
         }; 
         _dataService.IsUserNameUnique(_User.UserName, _User.UserID, Database.CurrentClient.ClientID); 
        } 

       _dataService.UpdateStaffAndUsersCompleted += (s, e) => 
       { 
        BusyIndicator = false; 
        if (e.Success) 
        { 
         } 
         if (CloseForm) 
          ReturnToHomePage(); 
         else 
         { 

          LoadUsers(); 
          OnUsersSaved(); 
         } 
        } 

       BusyIndicator = true; 
       BusyMessage = "Saving..."; 
          _dataService.UpdateUsers(Database.CurrentProject.ProjectID, Database.CurrentClient.ClientID, _UpdatedUsers, _DeletedProjectUsers); 
      } 

在這種情況下,我正在嘗試查找用戶名是否唯一,向用戶顯示消息並返回。 顯然它不是那麼簡單。我嘗試了更多不同的方式,但它不起作用。我如何得到這個工作?

+0

如果您的用戶IsUnique是否想避免UpdateStaffAndUsers()調用? – bit

+0

是的..如果其中一個用戶不是唯一的我不希望UpdateStaffAndUsers()函數運行 –

+0

然後放置「_dataService.UpdateUsers(Database.CurrentProject.ProjectID,Database.CurrentClient.ClientID,_UpdatedUsers,_DeletedProjectUsers);」在CheckIsUserNameUnique處理程序內部 – bit

回答

1

我想你可以通過添加一些輔助函數來讓你的生活更輕鬆。第一個是檢查用戶是否唯一的異步函數。如果出現錯誤,您可能需要添加一些代碼來設置tcs.SetException

private Task<bool> IsUserUniqueAsync(UserViewModel user, DatabaseServiceLocal dataService) 
{ 
    var tcs = new TaskCompletionSource<bool>(); 

    dataService.CheckIsUserNameUnique += (s, e) => 
         { 
          tcs.SetResult(e.IsUnique); 
         }; 
    dataService.IsUserNameUnique(user.UserName, user.UserID, Database.CurrentClient.ClientID); 
    return tcs.Task; 
} 

第二個更新所有用戶asynchrnously

public Task<bool> UpdateUsersAsync(ObservableCollection<User> updatedUsers, DatabaseServiceLocal dataService) 
{ 
    var tcs = new TaskCompletionSource<bool>(); 

    BusyIndicator = true; 
    BusyMessage = "Saving..."; 

    dataService.UpdateStaffAndUsersCompleted += (s, e) => 
       { 
        BusyIndicator = false; 
        tcs.SetResult(e.Success);     
       }; 

    dataService.UpdateUsers(Database.CurrentProject.ProjectID, Database.CurrentClient.ClientID, updatedUsers, _DeletedProjectUsers); 

    return tcs.Task; 
} 

那麼你SaveUsers方法變得簡單一點。

private async void SaveUsers(bool CloseForm) 
{ 
    ObservableCollection<User> _UpdatedUsers = new ObservableCollection<User>(); 
    DatabaseServiceLocal _dataService = new DatabaseServiceLocal(Database); 
    Dictionary<Task<bool>, User> tasks = new Dictionary<Task<bool>, User>(); 

    // start all tasks in parallel 
    foreach (UserViewModel _User in _AllUsers) 
    { 
     if (_User.Dirty && !_User.IsBlank) 
     { 
      tasks.Add(IsUserUniqueAsync(_User, _dataService), _User);  
     } 
    }   

    // process each task as it completes 
    while(tasks.Count() > 0) 
    { 
     var task = await Task.WhenAny(tasks.Keys.ToArray()); 

     if(task.Result) 
     { 
      _UpdatedUsers.Add(_User.SaveAsUser()); 
     } 
     else 
     { 
      MessageBox.Show(string.Format("Username {0} is not allowed as it already exists in the system. Please choose a different username.", ""), null); 
      return; 
     } 

     tasks.Remove(task); 
    } 

    if(await UpdateUsersAsync(_UpdatedUsers, _dataService)) 
    { 
     if (CloseForm) 
      ReturnToHomePage(); 
     else 
     { 

      LoadUsers(); 
      OnUsersSaved(); 
     } 
    } 
} 
+0

我的不好...編輯了這個問題的代碼,並刪除他們出於某種原因。謝謝指出。 –

1

您的代碼將或多或少看起來像這樣。

ObservableCollection<User> _UpdatedUsers = new ObservableCollection<User>(); 
int _verifiedUsersCount = 0; 
DatabaseServiceLocal _dataService = new DatabaseServiceLocal(Database); 

//Verify unique users 
private void SaveUsers(bool CloseForm) 
{ 

    _dataService.CheckIsUserNameUnique += CheckIsUserNameUnique; 

    foreach (UserViewModel _User in _AllUsers) 
    { 
     //bool success = _dataService.IsUserNameUnique(_User.UserName, _User.UserID, Database.CurrentClient.ClientID); 
     if (_User.Dirty && !_User.IsBlank) 
     { 
      _dataService.IsUserNameUnique(_User.UserName, _User.UserID, Database.CurrentClient.ClientID); 
     } 
    } 
} 

//Store verified users to save 
private void CheckIsUserNameUnique(object s, CheckIsUserNameUniqueEventArgs e) 
{ 
    if (e.IsUnique) 
     _UpdatedUsers.Add(_User.SaveAsUser()); 
    else 
    { 
     csaMessageBox.Show(string.Format("Username {0} is not allowed as it already exists in the system. Please choose a different username.", ""), null); 
    } 
    verifiedUsersCount++; 

    //Call after all the users have been verified for uniqueness 
    if (_AllUsers.Count() == verifiedUsersCount) 
    { 
     OnUniqueUserVerifyComplete(); 
    } 
} 

//Save verified users 
private void OnUniqueUserVerifyComplete() 
{ 
    //No unique users 
    if (_UpdatedUsers.Count < 1) { return; } 

    _dataService.UpdateStaffAndUsersCompleted += (s, e) => 
    { 
     BusyIndicator = false; 
     if (e.Success) 
     { 
     } 
     if (CloseForm) 
      ReturnToHomePage(); 
     else 
     { 
      LoadUsers(); 
      OnUsersSaved(); 
     } 
    }; 

    BusyIndicator = true; 
    BusyMessage = "Saving..."; 
    _dataService.UpdateUsers(Database.CurrentProject.ProjectID, Database.CurrentClient.ClientID, _UpdatedUsers, _DeletedProjectUsers); 
}