Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working on a WPF application with telerik controls.

I'm using RadListBox which is bind to the collection. When i select each RadListBoxItem a detailed view of the BindedItem will be shown in the nearby panel. Only one RadListBoxItem can be selected at a time.

In the below logic i'm making the following functionality,

  1. Edit
  2. Switch to new
  3. Alert for Save or Discard
  4. Save/Discard
  5. Load the selected

Now the issue is When the alert is thrown for Save/Discard, if i click save then the save process is initiated but before the save completed the load is also running in another thread. I have to check if the save is complete and start the load process.

//XAML Code:

<telerik:RadListBox x:Name="lstMarketSeries" ItemsSource="{Binding SCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, ValidatesOnDataErrors=True}" SelectedItem="{Binding SelectedMSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" telerik:StyleManager.Theme="Windows8"> 
<i:Interaction.Triggers>
   <i:EventTrigger EventName="SelectionChanged">
     <i:InvokeCommandAction Command="{Binding LoadSelected}"/>
   </i:EventTrigger>
</i:Interaction.Triggers>
</telerik:RadListBox> 

//ViewModel:

public ICommand LoadSelected { get { return new RelayCommand(LoadSelectedSDetails); } }

/// <summary>
/// Load selected series
/// </summary>
private async void LoadSelectedSDetails()
{
   if (SCollection != null)
   {
       if (!IsChanged())
       {
           bw = new BackgroundWorker();
           bw.RunWorkerAsync();

           bw.DoWork += (s, e) =>
           {
                //Loading functionality here( Have to check if the save is complete)
           };

           bw.RunWorkerCompleted += (s, e) =>
           {
                IsBusy = false;
           };
       }
       else
       { 
           await PutTaskDelay();  // This delay is to wait for save to complete
           LoadSelectedSDetails();
       }

/// <summary>
/// Check if collection is changed
/// </summary>
private bool IsChanged()
{
    bool IsChanged = false;
    if (SCollection != null)
       IsChanged = SCollection.Where(x => x.IsChanged || x.IsNew).Count() > 0;

    if (IsChanged)
    {
       if (ShowMessages.SaveOrDiscardBox())
       {
          SaveAllDetails(); // Saving runs in a separate thread.
       }
       else
       {
          //Discard functionality goes here
       }
    }

    return IsChanged;
}

Kindly help on this issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
239 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you want to do something after a Task is executed you can use ContinueWith:

       var saveTask = new Task<ReturnedObject>(() =>  //if you don't need values in order to update the UI you can use Task not Task<T>
                    {
                      //do your save action and return something if you want 
                    });

                    //start thread
                    saveTask.Start();

                    saveTask.ContinueWith(previousTask =>
                    {//after the first thread is completed, this point will be hit

                        //loading action here

                        //UI region if needed
                            Application.Current.Dispatcher.BeginInvoke((ThreadStart)delegate
                            {
                              //update UI
                            }, DispatcherPriority.Render);               

                    }, TaskScheduler

    .FromCurrentSynchronizationContext());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...