本文介绍了与backgroundworker异步打开第二个winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个名为 login 的winform,询问用户名和密码,用数据库检查信息(在线),然后显示 主winform ,但为了避免应用程序冻结,我使用了两个 backgroundworker 。 在我的主winform上,我有几个标签需要连接到数据库才能加载一些数据,但是一旦我更改标签当我点击我的登录按钮时,所选的第一个标签显示 一半或冻结 : private void btncon_Click ( 对象 发件人 , EventArgs e ) { strLogin = tblogin 。 文字 。 修剪 (); pass = tbpwd 。 文字 ; 如果 (! backgroundWorker1 。 IsBusy ) backgroundWorker1 。 RunWorkerAsync ( ); } 和 private void backgroundWorker1_DoWork ( 对象 发件人 , DoWorkEventArgs e ) { string [] 用户 = cm 。 login ( strLogin , pass ); int dataload = 0 ; 如果 ( 用户 [ 0 ] != null ) { 成功 = true ; } else { 成功 = false ; } } 所以当 backgroundworker1 已经完成,我打电话给 backgroundWorker2 private void backgroundWorker1_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e ) { 如果 ( 成功 ) { backgroundWorker2 。 RunWorkerAsync (); } else { 此 。 显示 (); } } 和 main Principale ; //主要表格 private void backgroundWorker2_DoWork ( object sender , DoWorkEve ntArgs e ) { Principale = new main (); } private void backgroundWorker2_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e ) { this 。 隐藏 (); / / hide login winform Principale 。 显示 (); // show mainform } 解决方案 >> ...一旦我更改标签页,所选的第一个标签显示为一半或冻结 要解决它,你可以尝试添加一个新的后台工作来执行操作加载数据: bool changed = false; private void tabControl1_Selected(object sender,TabControlEventArgs e) { changed = true; if(!backgroundWorker1.IsBusy) { if(changed) { backgroundWorker1.RunWorkerAsync(); } } } private void backgroundWorker1_DoWork(对象发送者,DoWorkEventArgs e) { //模拟耗时的操作从DB 中加载数据int sum = 0; for(int i = 0; i< int.MaxValue; i ++) { sum + = i; } textBox1.Text = sum.ToString(); //模拟从DB上加载数据的耗时操作 changed = false; } 问候, Stanly Hi ,i have a winform called login that asks username and password , checks the information with a database(online) and then displays themain winform , but to avoid that the application freezes , i used twobackgroundworker.On my main winform I have several tabs that need the connection to the database to load some data but as soon as I change tab the first tab selected appearshalf or freezewhen I click on my login button : private void btncon_Click(object sender, EventArgs e) { strLogin = tblogin.Text.Trim(); pass = tbpwd.Text; if (!backgroundWorker1.IsBusy) backgroundWorker1.RunWorkerAsync(); }and private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string[] user = cm.login(strLogin, pass); int dataload = 0; if(user[0] != null) { success = true; } else { success = false; } }so when backgroundworker1 has finished , i call backgroundWorker2private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (success) { backgroundWorker2.RunWorkerAsync(); } else { this.Show(); } }and for main Principale; //main formprivate void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e){ Principale = new main();} private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.Hide(); //hide login winform Principale.Show(); //show mainform } 解决方案 Hi,>>… as soon as I change tab the first tab selected appears half or freezeI guess this problem might be caused by time-consuming operations, such as loading data from database.To solve it, you can try to add a new backgroundworker to perform the operation of loading data: bool changed = false; private void tabControl1_Selected(object sender, TabControlEventArgs e) { changed = true; if (!backgroundWorker1.IsBusy) { if (changed) { backgroundWorker1.RunWorkerAsync(); } } } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // time-consuming operations to simulate loading data from DB int sum = 0; for (int i = 0; i < int.MaxValue; i++) { sum += i; } textBox1.Text = sum.ToString(); // time-consuming operations to simulate loading data from DB changed = false; }Regards,Stanly 这篇关于与backgroundworker异步打开第二个winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 15:14