先来一个加载窗体代码

  public partial class FrmLoading : Form
{
public BackgroundWorker updateDBWorker=new BackgroundWorker(); public Action BackgroundWorkAction
{
get;
set;
} public KeyValuePair<int, string> CurrentMsg
{
set
{
this.updateDBWorker.ReportProgress(value.Key, value.Value);
}
} public FrmLoading()
{
InitializeComponent();
this.updateDBWorker.WorkerReportsProgress = true;
this.updateDBWorker.WorkerSupportsCancellation = true;
this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
this.updateDBWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
lblVer.Text = Properties.Resources.SystemVer;
} public void ShowLog(string strLog, int intValue)
{
if (this.lblLog.InvokeRequired)
{
this.lblLog.BeginInvoke(new MethodInvoker(delegate() { ShowLog(strLog, intValue); }));
}
else
{
lblLog.Text = strLog;
this.progressBar1.Value = intValue;
}
} private void FrmLoading_Load(object sender, EventArgs e)
{
this.updateDBWorker.RunWorkerAsync();
} private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (this.BackgroundWorkAction != null)
{
this.BackgroundWorkAction();
}
Thread.Sleep();
if (base.InvokeRequired)
{
base.BeginInvoke(new MethodInvoker(delegate
{
base.Close();
}));
}
else
{
base.Close();
}
} private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ShowLog((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage);
} private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}

界面就一个进度条,一个label,没其他东西

看调用的地方,Program文件里面

 FrmLoading frmLoading = new FrmLoading();
frmLoading.BackgroundWorkAction = delegate
{
try
{
//设置连接字符串
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在初始化数据配置..."); frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在初始化日志配置...");
System.Threading.Thread.Sleep(); frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在升级本地数据..."); frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在初始化本地参数..."); frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在初始化热键信息..."); frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在初始化硬件设备..."); frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "初始化完成..."); }
catch (Exception ex)
{
Application.Exit();
Process.GetCurrentProcess().Kill();
}
};
frmLoading.ShowDialog(); Application.Run(new form1());

好了 就这样了,没什么技术含量,就不贴图了,拿去用吧

05-11 17:21