是否有其他方法可以在 WPF 中使用后台工作人员?

发布于 2024-11-29 07:09:37 字数 238 浏览 1 评论 0原文

我是 WPF 的初学者,在我的应用程序中,我需要执行一系列初始化步骤,这些步骤需要 10-15 秒才能完成,在此期间我的 UI 变得无响应。

我昨天使用了后台工作程序,但它没有更新我的窗口,事实上它被冻结了。不确定,但也许它不起作用,因为该控件仅适用于 Windows 窗体。

更新:

如果不是太麻烦,你能给我一个使用替代方案的例子吗?就我而言,程序将从数据库中快速获取一些值。

I am a beginner with WPF, in my application I need to perform a series of Initialization steps, these take 10-15 seconds to complete during which my UI becomes unresponsive.

I was using yesterday the background worker but it didn't update my window, in fact it was frozen. Not sure, but maybe it didn't work because this control is only for Windows Forms.

UPDATE:

If not too much trouble, can you post me an example to use the alternative? For my case, the program will get some values from a database in a blucle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心 2024-12-06 07:09:37

调度程序
调度程序为特定线程维护一个按优先级排列的工作项队列。这可能会帮助您更新 UI。如果您有很多与 UI 相关的初始化,那么即使这也无法为您提供太大帮助。

实际上,Dispatcher 并不总是 BackgroundWorker 的替代品。最佳实践是根据您的要求选择更合适的。例如,如果您希望在不排队的情况下执行某些操作,BackgroundWorker 就是解决方案。另一方面,如果排队不是问题,那么调度程序是一个替代方案。例如,Dispatcher 正在使用拼写检查器和语法突出显示功能。

WPF 线程模型< /p>

所有 WPF 应用程序都以两个重要线程开始,其中一个用于
渲染和管理用户界面的一个。渲染图
线程是在后台运行的隐藏线程,因此唯一的
您通常处理的线程是 UI 线程。 WPF 需要
它的大部分对象都与 UI 线程相关联。这被称为
线程亲和性,意味着您只能在线程上使用 WPF 对象
它是在其上创建的。在其他线程上使用它会导致
要抛出的运行时异常。请注意,WPF 线程模型
与基于 Win32® 的 API 可以很好地互操作。这意味着 WPF 可以
托管或由任何基于 HWND 的 API(Windows 窗体、Visual Basic®、
MFC,甚至 Win32)。

线程关联由 Dispatcher 处理
类,WPF 应用程序的优先级消息循环。通常您的
WPF 项目有一个 Dispatcher 对象(因此也有一个
UI 线程),所有用户界面工作都通过它进行。

笔记 :

Dispatcher和其他线程方法的主要区别
问题是 Dispatcher 实际上并不是多线程的。调度员
管理控件,这些控件需要单个线程才能正常运行;
Dispatcher 的 BeginInvoke 方法将事件排队以供稍后使用
执行(取决于优先级等),但仍在同一线程上。

请参阅此主题了解更多信息。

Dispatcher.
The Dispatcher maintains a prioritized queue of work items for a specific thread. This might help you for updating your UI. If you have a lot of UI related initializations even this won't be able to help you much.

Dispatcher is not always an alternative to BackgroundWorker actually. The best practice is to select the more appropriate one as per your requirement. For example if you want something to execute without queuing BackgroundWorker is the solution. On the other hand if queuing is not a problem then Dispatcher is an alternative. For example, Dispatcher is using in Spell checkers and syntax highlighting functionality.

WPF Thread Model

All WPF applications start out with two important threads, one for
rendering and one for managing the user interface. The rendering
thread is a hidden thread that runs in the background, so the only
thread that you ordinarily deal with is the UI thread. WPF requires
that most of its objects be tied to the UI thread. This is known as
thread affinity, meaning you can only use a WPF object on the thread
on which it was created. Using it on other threads will cause a
runtime exception to be thrown. Note that the WPF threading model
interoperates well with Win32®-based APIs. This means that WPF can
host or be hosted by any HWND-based API (Windows Forms, Visual Basic®,
MFC, or even Win32).

The thread affinity is handled by the Dispatcher
class, a prioritized message loop for WPF applications. Typically your
WPF projects have a single Dispatcher object (and therefore a single
UI thread) that all user interface work is channeled through.

NOTE :

The main difference between the Dispatcher and other threading methods
is that the Dispatcher is not actually multi-threaded. The Dispatcher
governs the controls, which need a single thread to function properly;
the BeginInvoke method of the Dispatcher queues events for later
execution (depending on priority etc.), but still on the same thread.

See this thread for more information.

就像说晚安 2024-12-06 07:09:37

您还可以使用线程池对项目进行排队并运行类似的任务,但要小心,如果您的任务完成后需要更新 UI,则必须将数据封送回 UI 线程。

You could also queue items up with the thread pool and run the tasks like that, but be careful, if your tasks need to update the UI when they are finished you will have to marshal the data back to the UI thread.

鹊巢 2024-12-06 07:09:37

可以使用异步委托。

http://msdn.microsoft.com/en-us/library/ms228963.aspx

只需确保您是否正在执行任何与 UI 相关的更新,请使用:

Dispatcher.CheckAccess()

这里是一个简单的示例:

private void HandleUIButtons()
{    
    if (!btnSplit.Dispatcher.CheckAccess())   
    {         
        //if here - we are on a different non-UI thread        
        btnSplit.Dispatcher.BeginInvoke(new Action(HandleUIButtons));    
    }    
    else        
    {
        btnSplit.IsEnabled = true; //this is ultimately run on the UI-thread
    }
}

取自此处:

http://blog.clauskonrad.net/2009/03/wpf-invokerequired-dispatchercheckacces .html

One could use asynchronous delegates.

http://msdn.microsoft.com/en-us/library/ms228963.aspx

Just make sure if you are doing any UI related updates use:

Dispatcher.CheckAccess()

Here a simple example:

private void HandleUIButtons()
{    
    if (!btnSplit.Dispatcher.CheckAccess())   
    {         
        //if here - we are on a different non-UI thread        
        btnSplit.Dispatcher.BeginInvoke(new Action(HandleUIButtons));    
    }    
    else        
    {
        btnSplit.IsEnabled = true; //this is ultimately run on the UI-thread
    }
}

Taken from here:

http://blog.clauskonrad.net/2009/03/wpf-invokerequired-dispatchercheckacces.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文