0

is there any solution for this scenario:

I do some time-consuming things on a new Thread (using Tasks). In this thread I want to update UI elements (text) so that the user knows what happens. This should happen at real-time, not only when a Task finished.

I always must explicitly call Dispatcher.Invoke() (it's the UI-Thread dispatcher) if I want to change something. Is there a way to run something asynchronously without having to invoke explicitly? I found a solution using the TaskFactory and specifying a TaskScheduler. But this locks the UI when UI-calls are made.

Is there a way to do this, without locking the UI?

EDIT: Because of some slight misunderstandings this edit. I want that once I call something like

uiControl.Text = "Test 123";

on the worker-thread to be automatically invoked on the UI-Thread if needed. So in this case it's needed, but I want to get rid of all these Invoke-calls. So if there is a nifty solution it would be great. Otherwise I have to use explicit invokes, not nice but ok.

SharpShade
  • 1,761
  • 2
  • 32
  • 45
  • 2
    If you are looking for asynchronous use `Dispatcher.BeginInvoke()`. – Rohit Vats Apr 07 '14 at 18:00
  • 1
    See [this related answer](http://stackoverflow.com/a/21884638/643085) – Federico Berasategui Apr 07 '14 at 18:19
  • Thanks, I know this. But this doesn't answer my question. I want to implicitly invoke. When I call something like "textbox.Text = 'Bla';" in the worker-thread it should automatically be invoked to the UI-Thread. That's what I wanted to know if it's possible ;) @HighCore: Your solution makes sense, but I got standard UI controls like the WPF textbox. Therefor I can't add custom NotifyProperChanged-calls for each property I may use... or I currently just don't get how this could help to solve my problem :D – SharpShade Apr 07 '14 at 18:35
  • @ZapStorm yes, you need a proper ViewModel which raises `PropertyChanged` for each relevant property by marshalling it to the UI thread as shown in my linked answer, regardless of what the UI elements are, that is the correct approach in WPF. Have you fully read my answer? You don't `textbox.Text = bla;` in WPF because you have to use proper databinding instead. – Federico Berasategui Apr 07 '14 at 18:47
  • Ok thanks, it's not the perfect solution, but it helps at least for some situations to reduce required invoke calls. Obviously there's no generic solution, but that's okay. Programming would be to easy if everything was automated :D – SharpShade Apr 07 '14 at 19:09

2 Answers2

1

PostSharp has this implemented as a method level aspect. What it means in terms of implementation is that you decorate a method with [DispatchedMethod] attribute and it will be dispatched to the UI thread every time it is called (by any thread). Take a look here for more details:

http://www.postsharp.net/aspects/examples/multithreading

http://www.postsharp.net/threading/thread-dispatching

http://doc.postsharp.net/##T_PostSharp_Patterns_Threading_DispatchedAttribute

Honza Brestan
  • 10,637
  • 2
  • 32
  • 43
  • Do you also know how these "Aspects" are made? The idea is pretty nifty and nice, but I really don't get how I get an Attribute to fetch invokes and dispatches them ... – SharpShade Apr 08 '14 at 12:38
  • Okay, as it seems this belongs into AOP (Aspect Oriented Programming) which is a bit overkill for my problem here. And I don't have the money for PostSharp, so I'll keep it basic. But thanks, I gathered a lot more knowledge :) – SharpShade Apr 08 '14 at 13:19
-2

You can do the operation on main thread and just call Application.DoEvents() from time to time in order to process UI events ...

Note that this only works for winforms so probably not a correct answer.

HighCore suggested this as a better answer https://stackoverflow.com/a/21884638/643085

Community
  • 1
  • 1
Bogdan
  • 484
  • 2
  • 7