日期:2014-05-18 浏览次数:20973 次
namespace WinAppThreadSuspendTest { public partial class Form1 : Form { private static Thread currentThread; public static Thread CurrentThread { get { return currentThread; } } public Form1() { InitializeComponent(); InitCurrentThread(); } private static void InitCurrentThread() { currentThread = Thread.CurrentThread; } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); currentThread.Suspend(); MessageBox.Show("Suspended thread resume"); } } }
namespace WinAppThreadSuspendTest { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { Form1.CurrentThread.Resume(); this.Close(); } } }
private static Thread MainThread; private static Frm_Login3 LoginForm; private static bool pass = false; public static Form ShowLoginForm(Type MainFormType, string description) { Form f = null; MainThread = Thread.CurrentThread; Thread t2 = new Thread(new ParameterizedThreadStart(CreateLoginForm));//创建登录线程 t2.Start(description);//启动登录线程 try { MainThread.Join();//挂起主线程,登录成功后在登录窗体上调用 MainThread.Interrupt();恢复主线程 } catch { } if (pass)//pass:是否登录成功 { try { f = (Form)Activator.CreateInstance(MainFormType);//创建主窗体 LoginForm.Invoke(new MethodInvoker(CloseLoginForm));//关闭登录窗体 } catch(Exception ex) { gt.WinForm.ShowMessage.Error(ex); f = null; } } return f; } private static void CloseLoginForm() { LoginForm.Close(); } private static void CreateLoginForm(object obj) { LoginForm = new Frm_Login3(); LoginForm.Description = obj.ToString(); LoginForm.ShowDialog(); }