c# - Closing the Form opened in another thread -
c# - Closing the Form opened in another thread -
i'm getting troubles winforms c# app. wish create form named popup
closing after operations in main thread done. problem exception caused cross-thread form closing.
private void loginbutton_click(object sender, eventargs e) { loginprocess.start(); // running form.show() in new thread activeacc.isvalid = false; activeacc.username = userbox.text; seek { logincheck(userbox.text, passbox.text); } grab (ioexception) { messagebox.show(".."); return; } grab (socketexception) { messagebox.show(".."); return; } if (activeacc.isvalid) { messagebox.show(".."); close(); } else { popup.close(); // error caused closing form different thread messagebox.show(""); } } public login() // 'main' form constructor { initializecomponent(); activeacc = new account(); popup = new loginwaiter(); loginprocess = new thread(popup.show); //popup ordinary form }
i've been trying utilize various tools such loginprocess.abort()
or popup.dispose()
create work properly, if app working on runtime environment still unstable due exceptions thrown. grateful help, , sorry ambiguities in issue describing.
why don't allow ui thread ui stuff opening , closing forms, , spawn other thread (or background worker, or async task) other stuff?
imo, having other threads effort interact elements on ui thread (e.g., have background thread straight set text of label or such) asking heartache.
if must maintain code is, here simple thing do. in popup, add together static bool defaults true. in popup, add together timer task 1 time every x milliseconds checks status of boolean. if finds value has been set false, allow popup tell close within timer tick.
i'm not crazy design, like:
public partial class popup : form { public static bool stayvisible { get; set; } private system.windows.forms.timer timer1; public popup() { stayvisible = true; this.timer1.interval = 1000; this.timer1.tick += new system.eventhandler(this.timer1_tick); initializecomponent(); } private void timer1_tick(object sender, eventargs e) { if (!stayvisible) this.close(); } }
then, thread, when want popup close, call
popup.stayvisible = false;
better yet, fire event popup receive close itself. since intend utilize multiple threads, you'll have deal raising events cross-thread.
c# multithreading winforms
Comments
Post a Comment