开发者

Update instance in the first method before continue to run the second method in the same command

开发者 https://www.devze.com 2023-04-10 17:06 出处:网络
I have a method like this public void LoadProgrammeListFromChannel(TVDailyScheduleParam scheduleParam, Action 开发者_开发问答callback)

I have a method like this

public void LoadProgrammeListFromChannel(TVDailyScheduleParam scheduleParam, Action 开发者_开发问答callback)
        {
            string url = Helper.GetProgrammeUrl(scheduleParam.Day, scheduleParam.Channel.Id); //1//
            WebClient client = new WebClient(); //2//

            client.OpenReadCompleted += new OpenReadCompletedEventHandler((sender, e) => //3//
            {//5//
                if (e.Error != null)
                    return;

                try
                {
                    _programmeList.Clear();
                    _programmeList = DataService.GetProgrammeList(e.Result);
                    // call method in MainVM to update View
                    callback();
                }
                finally
                {
                    // close file stream
                    e.Result.Close();
                }
            });

            client.OpenReadAsync(new Uri(url, UriKind.Absolute)); //4//
        }

and I have a cammand like this

LoadWhatsonProgrammeCommand = new RelayCommand(()=>
            {
                foreach (TVDailyScheduleParam param in _tvDailyScheduleVM.ChannelList.Select(c => new TVDailyScheduleParam(DateTime.Today, c, false)))
                {
                    TVDailyScheduleParam param2 = param;

                    _tvDailyScheduleVM.LoadProgrammeListFromChannel(param2, ()=>
                    {
                        RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
                    });

                    _tvDailyScheduleVM.GetWhatsonProgramme(param2, ()=>
                    {
                        RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
                    });
                }
            });

Now when I invoke the command. At first it runs _tvDailyScheduleVM.LoadProgrammeListFromChannel and invoke the LoadProgrammeListFromChannel method.

In the LoadProgrammeListFromChannel method it runs from 1 -> 2 -> 3. At 3, it's not completed sothat it runs to 4 and then back to the command and continue to run _tvDailyScheduleVM.GetWhatsonProgramme.

But the _programmeList in LoadProgrammeListFromChannel is not updated so that GetWhatsonProgramme does not run exactly.

How can I go back to LoadProgrammeListFromChannel in order to run 3 to update _programmeList before running _tvDailyScheduleVM.GetWhatsonProgramme ?


The behaviour you describe is "by design". Your method LoadProgrammeListFromChannel makes an asynchroneous call. This means within your method the sequence is 1 -> 2 -> 3 -> 4 -> return -> execute code in calling function -> then sometime later 5 and then the callback.

As a result LoadProgrammeListFromChannel and GetWhatsonProgramme are executed in parallel. So if GetWhatsonProgramme needs always to run after LoadProgrammeListFromChannel you will have the call to move GetWhatsonProgramme into your callback method, i.e.

LoadWhatsonProgrammeCommand = new RelayCommand(()=> {
    var channelList = _tvDailyScheduleVM
                          .ChannelList
                          .Select(c => new TVDailyScheduleParam(
                              DateTime.Today, 
                              c, 
                              false
                          ));

    foreach (TVDailyScheduleParam param in channelList) {
        TVDailyScheduleParam param2 = param;

        _tvDailyScheduleVM.LoadProgrammeListFromChannel(param2, ()=> {
            RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);

            _tvDailyScheduleVM.GetWhatsonProgramme(param2, ()=> {
                RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
            });
        });
    }
});

Alternatively, you could subscribe to the PropertyChangedEvent of your ViewModel and if the TV_DAILY_SCHEDULE_VM property has been changed, call the GetWhatsonProgramme from there, although this might not be desireable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号