c# - STA for non created Thread/Task -


i made simple async method loading animation while code runs. code runs adds controls stackpanel.

in page_loaded event have:

test(); 

in test() have

private async void test() {    testbtn.visibility = visibility.visible;    await task.run(() => loadall());    testbtn.visibility = visibility.collapsed; } 

and in loadall() dockpanels etc. exmaple:

dockpanel headerpanel = new dockpanel(); headerpanel.lastchildfill = false; 

when running error because thread isnt sta thread. how can make sta if dont created thread. (just have await task.run(() => loadall());)

do have create big thread etc. simple animation bar while code runs?

you can run task in scheduler allows safely execute ui code:

private async void test() {     testbtn.visibility = visibility.visible;     taskscheduler scheduler = taskscheduler.fromcurrentsynchronizationcontext();     await task.factory.startnew(() => loadall(),         cancellationtoken.none, taskcreationoptions.none, scheduler);     testbtn.visibility = visibility.collapsed; } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -