c# - WPF open new window on another thread -
in code below, call method opens custom new window. when application doing long running task wish still able activate window. possible on thread or using task
class?
public static class customw { static custom_window_chrome_demo.themedwindow msgbox(string msgbx_ttl, string msgbx_contnt) { var w_mbx = new custom_window_chrome_demo.themedwindow(); w_mbx.width = 950; w_mbx.height = 159; w_mbx.title = msgbx_ttl; grid g = new grid(); stackpanel spm = new stackpanel(); textblock tblckerrmsg = new textblock(); //more settings...... } }
this how tried invoke it,
public void newmsgboxshow(string msgbx_ttl, string msgbx_contnt) { system.threading.thread s = new system.threading.thread( ()=> customw.msgbox(msgbx_ttl, msgbx_contnt).show() ); }
but when using new thread getting following error.
the calling thread must sta, because many ui components require this
what correct way achieve required result ?
use this:
task.factory.startnew(new action(() => { //your window code }), cancellationtoken.none, taskcreationoptions.none, taskscheduler.fromcurrentsynchronizationcontext());
when new thread created current synchronization context able update ui.(when current thread ui thread)
you can use dispatcher execute code.
system.windows.application.current.dispatcher.begininvoke(new action(()=> { //your window code }));
Comments
Post a Comment