c# - Task.Wait vs Task.RunSyncronously where task has call to WPF Dispatcher.Invoke -


I have a task that I'm starting and ending in the WPF app I want to wait. Inside this work I call a verb on the dispatcher.

If I Tasks. I use. Waiting () It appears that the method has never expired. In addition, breakpoints inside the dispatcher Invoke never hits.

If I Tasks. I use. Sensational () This works correctly and breakpoints are hit within the dispatcher.

Why is there any difference?

Sample the code below:

  Public Zero ExampleMethod () {// Following: var work = New workload (LoadStuff); // It never returns: Work. Start (); Task.Wait (); // This version, however, does: task.RunSyncronously (); } Private Zero LOADSTOFF () {Observable Collection & lt; StuffObj & gt; Accessories = materials. Lod (arg1, true); Dispatch Helper.RunOnDispTouch ((= = {...}); } Public Static Class Dispatches {Public Static Zero Runon Dispatcher (Action Action) {Application.Server.Dispactor. Invoke (action); }}    

Yes, a big difference is if you RunSyncronously You only run the work in the UI thread. If you start it in a background thread and we have wait then the code is running in the background thread and the UI thread is blocked if the code is being applied to the UI thread within that function, and the UI Blocking the thread (from waiting ), you have created a detention, and the app will remain stable.

Note that if you have used, run synchronousrescreens is from a non-UI thread on that task, and the UI thread is being blocked by something else You will still see the deadlock.

Now, what you should do, here are actually two options:

  1. This task does not really take a long time, Instead of a background thread, the UI Thread UI thread will not be temporarily (temporarily) to stabilize so that it can all work directly in the UI. If this is the case, then maybe you should not even do this work, just put the code into a method and call the method.

  2. The task takes a long time to run, and then it updates the UI after doing that work. If this is the case then it is important that this RunSyncronously but not in the background thread. To prevent your entire application from deadlock, this will mean that you have to block not the UI thread through the call. If you have some code that you want to run after the work is done, then what do you have to do, add continuity to the job. C # 4.0 can be done by calling this service on on and adding it to a delegate to run. In C # 5.0+ you can do wait instead of related tasks (instead of waiting ing, which is actually a big difference) and it will automatically be used for the remainder Wired up will give you the method of continuing to run as a continuation (in fact it is a clear continue syntax for the call, but it is very useful).

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -