定义变量
private bool bRetrun = false;
private ManualResetEvent meDo;
定义一个可以公用的静态变量
public class CommonData
{
public static bool CloseFrom { get; set; }
}
主程序
Thread t = new Thread(new ThreadStart(TestDB));
meDo = new ManualResetEvent(false);
t.Start();
this.Hide();
if (new frmCloes().ShowDialog() == DialogResult.OK)
{
this.Show();
}
meDo.WaitOne();
if (bRetrun) return;
把会卡死的程序放到方法里执行,比如TestDB
private void TestDB()
{
if (!SysConfig.DBConnectionRing())
{
MessageBox.Show("选择的数据库连接失败,请检查!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
bRetrun = true;
}
else
{
bRetrun = false;
}
meDo.Set();
CommonData.CloseFrom = true;
}
创建一个用于显示进程的窗体,做门面
在该窗体添加一个label和一个timer
private int RunCount = 0;
private void tmr_Tick(object sender, EventArgs e)
{
if (CommonData.CloseFrom)
{
this.DialogResult = DialogResult.OK;
Close();
}
else
{
RunCount++;
if (RunCount > 10)
{
lblStatus.Text = "操作正在进行,请稍后";
RunCount = 0;
}
lblStatus.Text += ".";
}
}