并发多线程C# [英] Concurrent Multi-Threading C#

查看:373
本文介绍了并发多线程C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Telerik控件的WPF应用程序.

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

我正在使用绑定到集合的RadListBox.当我选择每个RadListBoxItem时,附近面板中将显示BindedItem的详细视图.一次只能选择一个RadListBoxItem.

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. 编辑
  2. 切换到新
  3. 保存或放弃的提醒
  4. 保存/放弃
  5. 加载所选的

现在的问题是,当引发保存/丢弃"警报时,如果我单击保存",则将启动保存过程,但在保存完成之前,负载也正在另一个线程中运行.我必须检查保存是否完成并开始加载过程.

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代码:

<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:

//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.

推荐答案

如果要在执行Task之后执行某些操作,可以使用ContinueWith:

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

这篇关于并发多线程C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆