DispatcherTimer WPF异步

时间:2016-07-25 12:33:38

标签: c# wpf asynchronous .net-3.5

每次定时器调用UpdateDocumentsListFromServer UI冻结3秒钟。如何在.net 3.5下以异步方式更新列表?

视图模型:

public class ShippingDocumentsRegisterViewModel : ViewModelBase
    {
        ShippingDocumentsModel model = new ShippingDocumentsModel();

        DispatcherTimer timer = new DispatcherTimer();

        BackgroundWorker BW = new BackgroundWorker();

        public ShippingDocumentsRegisterViewModel()
        {
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(UpdateDocumentsListFromServer);
            timer.Interval = new TimeSpan(0, 0, 10);
            timer.Start();

            this.Columns = model.InitializeColumns();
            BW.DoWork += UpdateDocumentsList;
            BW.RunWorkerAsync();
        }

        public void UpdateDocumentsList(object o, EventArgs args)
        {
            this.ShippingDocuments = model.GetDocuments();
        }

        public void UpdateDocumentsListFromServer(object o, EventArgs args)
        {
            // Taking a lot of time. How to do it async?
            var tempDocuments = model.GetDocumentsFromServer();
            foreach (var item in tempDocuments)
            {
                this.shippingDocuments.Add(item);
            }
            //
        }

        private ObservableCollection<ShippingDocument> shippingDocuments;

        public ObservableCollection<ShippingDocument> ShippingDocuments
        {
            get
            {
                return shippingDocuments;
            }

            private set
            {
                shippingDocuments = value;
                RaisePropertyChanged("ShippingDocuments");
            }
        }

        public ObservableCollection<ShippingDocumentColumDescriptor> Columns { get; private set; }

    }

GetDocumentsFromServer看起来像

    public ObservableCollection<ShippingDocument> GetDocumentsFromServer()
    {
        System.Threading.Thread.Sleep(3000);
        return new ObservableCollection<ShippingDocument> { new ShippingDocument { Name = "Test" } };
    }

4 个答案:

答案 0 :(得分:1)

您还可以使用向UI报告进度的后台工作程序

public ShippingDocumentsRegisterViewModel()
{

    BW.DoWork += UpdateDocumentsListFromServer;
    BW.RunWorkerCompleted += BW_RunWorkerCompleted;

    BW.WorkerReportsProgress = true;
    BW.ProgressChanged += UpdateGui;
    BW.RunWorkerAsync();
}
public void UpdateGui(object o, EventArgs args)
{
    foreach (var item in tempDocuments)
    {
        this.shippingDocuments.Add(item);
    }
}
public void UpdateDocumentsListFromServer(object o, EventArgs args)
{

    while (true) {
        System.Threading.Thread.Sleep(3000);

        tempDocuments = GetDocumentsFromServer();
        BW.ReportProgress(0);

    }
}

int num = 0;
public ShippingDocument[] GetDocumentsFromServer()
    {
        System.Threading.Thread.Sleep(3000);
        return new ShippingDocument[1] { new ShippingDocument { Name = "Test" + num++} };
    }

private ShippingDocument[] tempDocuments = new ShippingDocument[0];

答案 1 :(得分:0)

使用常规shippingDocuments,仅发送对"a","b","c","d",",","f" 的访问权限。

答案 2 :(得分:0)

如评论中所述,您可以使用Timers代替DispatcherTimerDispactherTimer将访问UIThread,其中Timer使用与线程池不同的线程。

此外,您可以从不同的线程

向UIThread发送操作
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
           {
               //Do some UI stuffs                   
           }));

希望有所帮助。

答案 3 :(得分:0)

使用Task和Async / Await将其卸载到新线程,如下所示:

public async void UpdateDocumentsListFromServer(object o, EventArgs args)
        {
            // This will execute async and return when complete
            await Task.Run(()=>{
              var tempDocuments = model.GetDocumentsFromServer();
              foreach (var item in tempDocuments)
              {
                  this.shippingDocuments.Add(item);
              }
            });
            //
        }

请记住此更新在不同的线程上然后是UI。因此,不允许触摸UI线程上的任何内容,否则您将遇到线程问题。因此,如果shippingDocuments是在UI线程上创建的并且不是线程安全的,那么您可以返回一组项目然后添加它们:

public async void UpdateDocumentsListFromServer(object o, EventArgs args)
        {
            // Execute on background thread and put results into items
            var items = await Task.Run(()=>{
              var tempDocuments = model.GetDocumentsFromServer();                  
              return tempDocuments;
            });
            //add occurs on UI thread.
            this.shippingDocuments.AddRange(tempDocuments);
        }