Async in WPF

This is a just simple summary about Async in WPF

  1. UI Thread will lock all the ViewModel which is used by it. So, any thread try to access to the ViewModel will throw an exception.
    But Function can be shared by other thread.

Reminder: if any functions or event_hanlder will operate the UI Control, we had better not call them in a non-UI thread. If we really need to do this job This function can help Application.Current.Dispathcer.Invoke(Action).

  1. If we define some functions which is time consuming, and we need to execute them in a seperate thread, it is a good pratice to pass all the variables through parameters. We should not suppose that, we can access the objects defined in the ViewModel.

  2. A simple summary:
    3.1 UI Thread have root right, It can access to any objects. and it will lock the objects which is used by itself. if any other thread hope to access to it, they may need to call Application.Current.Dispatcher.Invoke(Action).
    3.2 aysnc function should be stand alone, all the data is through parameter.
    3.3 all ui controls access need the help of Application.Current.Dispatcher.Invoke(Action).

posted @ 2017-05-18 14:52  kongshu  阅读(141)  评论(0编辑  收藏  举报