c# - Why don't WinForms/WPF controls use Invoke internally? -


i understand why gui controls have thread affinity.

but why don't controls use invoke internally in methods , properties?

now have stuff update textbox value:

this.invoke(new methodinvoker(delegate() {     textbox.text = "newvalue"; } 

while using textbox.text = "newvalue"; enough represent same logic.

all have done change textbox.text logic (pseudocode):

public string text {     set     {         if(!this.invokerequired)             // change logic         else             throw new badthreadexception();     } } 

to this:

public string text {     set     {         if(!this.invokerequired)             // change logic         else             this.invoke(new methodinvoker(delegate()             {                 // change logic             }     } } 

the same goes getters , methods.

i'm not proposing remove invoke/begininvoke, i'm asking why controls don't necessary thread switch instead of throwing exception.

i think way api enforces developers make explicit decision , avoid unintentional programming mistakes. here several problems come right away:

1. unintentional thread blocking. if write property, calling thread have block until message processed ui thread. , if calling thread owns resource ui thread might want acquire you'll deadlock hard debug (calling thread holds resource, , wait until message processed ui thread; ui thread waits until resource released).

2. unexpected surprises. if make write operations implicitly asynchronous run situation reader should never expect values date.

3. performance impact. if write intensive algorithm uses ui dispatching implicitly, end poor performance , free blame framework developers. after wrote sorting should run in o(n), reason takes ages complete.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -