c# - Why does Dispatcher.Invoke not execute the delegate argument in this example? -
[test] public void a() { var d = dispatcher.currentdispatcher; action action = () => console.writeline("dispatcher invoked me!"); var worker = new backgroundworker(); worker.dowork += somework; //worker.runworkerasync( (action) delegate { console.writeline("this works!"); } ); worker.runworkerasync((action) delegate { d.invoke(action); } ); system.threading.thread.sleep(2500); } private void somework(object sender, doworkeventargs e) { (e.argument action)(); }
this block of code doesn't throw exception. @ same time, dispatcher.invoke nothing. found odd.
i extracted helper method base viewmodel. worker threads used method doonuithread() avoid thread affinity issue. in unit-tests, find attempting test view model objects results in failures due above issue.
i move whole behavior out pluggable dependency substitute in tests. e.g. viewmodelbase depends on uithreadexecutor.execute(action) , use fake calls action in tests. i'm curious why dispatcher behaves way does..
dispatcher can perform begin/invoke() duty when main thread goes idle , re-enters dispatch loop. @ point main thread quiescent , can safely execute dispatched requests. notifications sent windows.
it isn't idle in case, stuck inside of sleep(2500).
Comments
Post a Comment