WPF进度条与调度程序的更新 [英] WPF progress bar update with dispatcher

查看:63
本文介绍了WPF进度条与调度程序的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用调度程序更新进度条,但不知为何无法想到将调度程序放置在何处.调用以及在其中传递什么内容.

我正在尝试导入文件,需要使用进度条向用户显示导入了多少文件.

所以我有一位代表:

public delegate void DelegateA(ProgressClass progressClass);

立即调用委托并将函数传递给调用.

DelegateA(FunctionA);

因此,在导入每个文件时,它会调用FunctionA.

private void FunctionA(ProgressClass progressClass)
{
    **//Put dispatcher.invoke here?**
    progressbar.updateprogress(progressclass);
    progressbar.show();
}

progressclass具有两个属性,用于设置进度条的值(已处理的数量)和要处理的项目总数.

我不明白在InvokeMethod(THreadPriority,委托方法)中传递什么委托方法?

对不起,如果不清楚.

解决方案

如果您尝试从某些非UI线程更新UI,则可以执行以下操作.

//here progress bar is a UIElement
progressBar.Dispatcher.BeginInvoke(
           System.Windows.Threading.DispatcherPriority.Normal
           , new DispatcherOperationCallback(delegate
                   {
                       progressBar1.Value = progressBar1.Value + 1;
                       //do what you need to do on UI Thread
                       return null;
                   }), null);

此代码摘自一个不错的关于从后台线程更新UI的帖子

I am trying to update a progressbar using the dispatcher but somehow cannot think where to place the dispatcher.Invoke and what to pass inside it.

Im trying to import files and needs to show the user how many files are imported using the progress bar.

so I have a delegate:

public delegate void DelegateA(ProgressClass progressClass);

im calling the delegate and passing the function to call.

DelegateA(FunctionA);

so while importing each file it calls the FunctionA.

private void FunctionA(ProgressClass progressClass)
{
    **//Put dispatcher.invoke here?**
    progressbar.updateprogress(progressclass);
    progressbar.show();
}

The progressclass has two properties which sets the progressbar's value(how many have been processed) and total items to process.

I can't understand what delegate method to pass in InvokeMethod(THreadPriority, delegate method)?

Sorry, if something is not clear.

解决方案

If you are trying to update the UI from some non-UI thread you can do something like this..

//here progress bar is a UIElement
progressBar.Dispatcher.BeginInvoke(
           System.Windows.Threading.DispatcherPriority.Normal
           , new DispatcherOperationCallback(delegate
                   {
                       progressBar1.Value = progressBar1.Value + 1;
                       //do what you need to do on UI Thread
                       return null;
                   }), null);

This code is taken from a good post about updating UI from background thread

这篇关于WPF进度条与调度程序的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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