multithreading - How to reset a textbox in WPF in a button handler before doing something else? -
i have simple wpf button , textbox in wpf application(not using mvc or binding @ all). able following upon clicking button:
1) clear textbox
2) create result
3) assign result textbox
i used textbox.clear, textbox.text= string.empty, delegates , dispatcher approach like
private void button_click(object sender, routedeventargs e) { application.current.dispatcher.begininvoke(new action (clearreporttxtbox), dispatcherpriority.send); system.threading.thread.sleep(5000); runtest(); } private void clearreporttxtbox() { report_textbox.text = string.empty; }
none of them working correctly me. dispatcher method somehow working not wish. seems clear task queued , when actions in button click handler finished, come play , delete textbox, causes generated report , assigned textbox (created runtest in code above) deleted well. hence late delete action , eliminate whole result.
currently seems me clicking on button uithread blocks , takes control. dispatcher queue delete action next action after finishing button click.
is possible force delete @ beginning , rest? reach pause button activity , delete @ first action , continue rest of actions in button handler.
am doing wrong?
thank in advance.
the "dispatcher.begininvoke" kinda weird want do
all ui update has done on main thread. since "button_click" event executing on main thread, delegate push dispatcher can executed after button_click handle completed. that's why execution sequence becomes 1. gui freeze because thread.sleep 2. runtest 3. clearreporttextbox
guess can try sth following instead.
private void buttonbase_onclick(object sender, routedeventargs e) { clearreporttxtbox(); task.factory.startnew(runtest); } private void clearreporttxtbox() { mytextbox.text = string.empty; } private void runtest() { system.threading.thread.sleep(5000); if (dispatcher != null && !dispatcher.checkaccess()) { dispatcher.invoke(priority, ()=> mytextbox.text = "123"); } else { mytextbox.text = "123"; } }
Comments
Post a Comment