c# - Clicking WPF window created on 2nd thread causes it to stop responding -
in current project i'm creating wpf based progress window in second thread. see previous post details on how i'm doing this.
my delegate method open progress window on second thread looks this:
void showprogresswindow() { this.progresswindow = new progresswindow(); progresswindow.show(); //causes dispatcher shutdown when window closed progresswindow.closed += (s, e) => dispatcher.currentdispatcher.begininvokeshutdown(dispatcherpriority.background); //notifies other thread progress window open when dispatcher starts system.windows.threading.dispatcher.currentdispatcher.begininvoke(new func<bool>(_progresswindowwaithandle.set)); //starts dispatcher system.windows.threading.dispatcher.run(); //forces worker cancel work workerinstance.requestcancel(); }
so if user closes window while process still running, dispatcher shutdown, code continue dispatcher.run() next line work canceled. after progress window thread exit.
this works fine if window closed clicking x in top right of window. window closes , work canceled.
yet if click cancel button i've added window progress window not close, , stops responding. cancel button has simple click event handler calls close() on window.
private void cancelbutton_click(object sender, routedeventargs e) { this.close(); }
if set breakpoint inside method never ends being hit after click button.
the xaml button pretty standard
<button x:name="cancelbutton" content="cancel" grid.row="7" horizontalalignment="right" grid.column="1" margin="0,0,12,12" width="75" height="23" verticalalignment="bottom" click="cancelbutton_click" />
after messing more realized clicking anywhere in window, not cancel button, cause stop responding. when window first opens can drag around screen grabbing title bar, if click anywhere inside window freeze.
there couple of ways run code on ui thread... prefer way:
dispatcher.currentdispatcher.invoke(dispatcherpriority.normal, (action)delegate() { // open window here });
Comments
Post a Comment