Is there an alternative to use the Background Worker in WPF?

十年热恋 提交于 2020-01-13 09:38:09

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/7005193/is-there-an-alternative-to-use-the-background-worker-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!