简体   繁体   English

Silverlight中的嵌套异步函数

[英]Nested Asynchronous function in Silverlight

I am trying to call a nested Asynchronous function but I am not getting the required data. 我试图调用嵌套的异步函数,但我没有得到所需的数据。 Since I am using a wcf service with Silverlight I can only use Asynchronous functions. 由于我在Silverlight中使用wcf服务,因此我只能使用异步函数。

In my code I am saving a set of rows containing userdata. 在我的代码中,我保存了一组包含userdata的行。 Before I save it I need to check the username is unique. 在我保存之前,我需要检查用户名是否唯一。 Now I only need to find out the first one and then break out of loop and show a message to the user.for simplicity sake, I have stripped the function of all the extra data and this is how it looks 现在我只需要找出第一个然后突破循环并向用户显示一条消息。为简单起见,我已经删除了所有额外数据的功能,这就是它的外观

      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);
            }

In this case I am trying to find if the username is unique,show user a message and return. 在这种情况下,我试图找到用户名是否唯一,向用户显示一条消息并返回。 Obviously it's not as simple as that.I have tried a couple more different ways but it didn't work. 显然它不是那么简单。我尝试了几种不同的方式,但它没有用。 How do I get this working? 我该如何工作?

I think you can make your life easier by adding a couple of helper functions. 我认为通过添加几个辅助函数可以让您的生活更轻松。 The first one is an asynchronous function that checks whether a user is unique. 第一个是异步函数,用于检查用户是否唯一。 You may need to add some code to set tcs.SetException if there is an error. 如果出现错误,您可能需要添加一些代码来设置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;
}

The second one updates all the users 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;
}

Then your SaveUsers method becomes a bit simpler. 然后你的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();
        }
    }
}

Your code would more or less look like this. 您的代码或多或少会像这样。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM