Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

wpf - Concurrent Multi-Threading C#

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[5] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...