后台复杂运算,加个进度条,进度条假死怎么解决 在后台运算的时候,想加个有进度条的窗体在前面循环滚动,可问题是后台的运算占据了系统资源,导致机器很卡,像死机一样,根本看不到进度条的效果,请问应该如何解决挨啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Application.DoEvents();应该加在哪里啊? for (int i=0;i<50000;i++){ 处理函数(); progressBar1.Value = i; Application.DoEvents(); } 注,上面的例子里progressBar1.Minimum = 0;progressBar1.Maximum = 50000; 我的一段多线程代码,如要通用的进度条源码,QQ:179781134 [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new UpdateForm()); } private void lnkShowMessage_Click(object sender, System.EventArgs e) { isSelectAdvance = !isSelectAdvance; if (isSelectAdvance) { //更换图片 向上双箭头 //扩大窗口 this.lblPic.Image = new Bitmap(this.GetType(),"ArrowUp.png"); lnkShowMessage.Text = "隐藏详细信息"; panMessage.Visible = true; this.MaximumSize = new Size(this.Width,this.Height + 160); this.MinimumSize = new Size(this.Width,this.Height + 160); //显示进度消息 } else { this.lblPic.Image = new Bitmap(this.GetType(),"ArrowDown.png"); lnkShowMessage.Text = "显示详细信息"; panMessage.Visible = false; this.MaximumSize = new Size(this.Width,this.Height - 160); this.MinimumSize = new Size(this.Width,this.Height - 160); } } private void UpdateForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (!isQuit) { this.WindowState = FormWindowState.Minimized; e.Cancel = true; } } private void notifyIconServer_DoubleClick(object sender, System.EventArgs e) { this.Show(); this.notifyIconServer.Visible = false; if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal; this.Activate(); } private void notifyIconServer_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {// if(e.Button==MouseButtons.Left)// {// Control control = new Control(null,Control.MousePosition.X,Control.MousePosition.Y,1,1);// control.Visible = true;// control.CreateControl();// Point pos = new Point(0,0);//这里的两个数字要根据你的上下文菜单大小适当地调整// this.contextMenuTray.Show(control,pos); // } } private void btnOK_Click(object sender, System.EventArgs e) {// this.btnOK.Enabled = false; ShowMessage("\n正在开始下载数据...",""); try { UpdateUtility.UpdateTable("tManager"); } catch(Exception ex) { MessageBox.Show(ex.Message); }// UpdateUtility.CloseConnectServer(); } private void btnQuit_Click(object sender, System.EventArgs e) {// DJ160API.DisableCard(); isQuit = true; this.notifyIconServer.Visible = false; this.Close(); } private void UpdateForm_SizeChanged(object sender, System.EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); this.ShowInTaskbar = false; this.notifyIconServer.Visible = true;//显示托盘图标 } } private void btnHide_Click(object sender, System.EventArgs e) { this.Close(); } private void ShowMessage(string lblMsg,string result) { if (!this.InvokeRequired) { //显示提示消息 lblMessage.Text = lblMsg; if (result != null && result != "") { ricMessage.SelectionBullet = true; ricMessage.AppendText(result); } } else { this.Invoke(new ShowMessageHandler(ShowMessage),new object[]{lblMsg,result}); } } delegate void SuccessHandler(string result,int type); private void ConnectedSuccess(string result,int type) { if (!this.InvokeRequired) { ShowMessage("",result); if (type == 0) //0 表示成功 -1表示失败 { btnOK.Enabled = true; MessageBox.Show("温馨提示\n\n 连接服务器成功,请按开始键进行数据更新!! ","消息", MessageBoxButtons.OK,MessageBoxIcon.Information); } if (type == 1) //0 表示成功 -1表示失败 { btnOK.Enabled = true; MessageBox.Show("温馨提示\n\n 服务器已是连接状态,请按开始键进行数据更新!! ","消息", MessageBoxButtons.OK,MessageBoxIcon.Information); } else if (type == -1) { btnOK.Enabled = false; MessageBox.Show("温馨提示\n\n 连接服务器失败,请重新连接... ","消息", MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } } else { this.Invoke(new SuccessHandler(ConnectedSuccess),new object[]{result,type}); } } private void ConnectServer() {// string conStr =@"Provider=SQLOLEDB.1;User ID=sa;Password=mysql;Initial Catalog=DB_Rental;Data Source=tcp:192.168.0.154\server2005,1433;"; string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DB_RENTAL.mdb;Persist Security Info=False"; try { int type = UpdateUtility.ConnectServer(conStr); ConnectedSuccess("\n服务器连接成功!!",type); } catch { ConnectedSuccess("\n连接服务器失败!!",-1); } } private void btnConnect_Click(object sender, System.EventArgs e) { //连接服务器 ConnectedHandler con = new ConnectedHandler(ConnectServer); con.BeginInvoke(null,null); } }} C#IPV6通讯编程 vs2008 c# 添加引用的问题 C#右键,指定listbox某一项右键响应菜单 做游戏需要用到socket技术,谁教教 求超大数的阶乘,比如求800的阶乘! 使用HttpWebRequest向服务器发送查询请求的时候提示连接被意外关闭 textbox手动绑定后运行两次就出问题,请指教[疑难杂症] [!!怪了!!]正则表达式 请问以下异常是什么原因产生的?? 请指教 控件被其它线程访问?"Cross-thread operation not valid" 可是我没有使用多线程啊 C# 怎样将html转换成xml
for (int i=0;i<50000;i++)
{
处理函数(); progressBar1.Value = i; Application.DoEvents();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new UpdateForm());
} private void lnkShowMessage_Click(object sender, System.EventArgs e)
{
isSelectAdvance = !isSelectAdvance;
if (isSelectAdvance)
{
//更换图片 向上双箭头
//扩大窗口
this.lblPic.Image = new Bitmap(this.GetType(),"ArrowUp.png");
lnkShowMessage.Text = "隐藏详细信息";
panMessage.Visible = true;
this.MaximumSize = new Size(this.Width,this.Height + 160);
this.MinimumSize = new Size(this.Width,this.Height + 160);
//显示进度消息
}
else
{
this.lblPic.Image = new Bitmap(this.GetType(),"ArrowDown.png");
lnkShowMessage.Text = "显示详细信息";
panMessage.Visible = false;
this.MaximumSize = new Size(this.Width,this.Height - 160);
this.MinimumSize = new Size(this.Width,this.Height - 160);
}
} private void UpdateForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!isQuit)
{
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
} private void notifyIconServer_DoubleClick(object sender, System.EventArgs e)
{
this.Show();
this.notifyIconServer.Visible = false; if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
this.Activate();
} private void notifyIconServer_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// if(e.Button==MouseButtons.Left)
// {
// Control control = new Control(null,Control.MousePosition.X,Control.MousePosition.Y,1,1);
// control.Visible = true;
// control.CreateControl();
// Point pos = new Point(0,0);//这里的两个数字要根据你的上下文菜单大小适当地调整
// this.contextMenuTray.Show(control,pos);
// }
} private void btnOK_Click(object sender, System.EventArgs e)
{
// this.btnOK.Enabled = false;
ShowMessage("\n正在开始下载数据...","");
try
{
UpdateUtility.UpdateTable("tManager");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
// UpdateUtility.CloseConnectServer();
} private void btnQuit_Click(object sender, System.EventArgs e)
{
// DJ160API.DisableCard();
isQuit = true;
this.notifyIconServer.Visible = false;
this.Close();
} private void UpdateForm_SizeChanged(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.ShowInTaskbar = false;
this.notifyIconServer.Visible = true;//显示托盘图标
}
} private void btnHide_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void ShowMessage(string lblMsg,string result)
{
if (!this.InvokeRequired)
{
//显示提示消息
lblMessage.Text = lblMsg;
if (result != null && result != "")
{
ricMessage.SelectionBullet = true;
ricMessage.AppendText(result);
}
}
else
{
this.Invoke(new ShowMessageHandler(ShowMessage),new object[]{lblMsg,result});
}
} delegate void SuccessHandler(string result,int type);
private void ConnectedSuccess(string result,int type)
{
if (!this.InvokeRequired)
{
ShowMessage("",result);
if (type == 0) //0 表示成功 -1表示失败
{
btnOK.Enabled = true;
MessageBox.Show("温馨提示\n\n 连接服务器成功,请按开始键进行数据更新!! ","消息",
MessageBoxButtons.OK,MessageBoxIcon.Information); }
if (type == 1) //0 表示成功 -1表示失败
{
btnOK.Enabled = true;
MessageBox.Show("温馨提示\n\n 服务器已是连接状态,请按开始键进行数据更新!! ","消息",
MessageBoxButtons.OK,MessageBoxIcon.Information); }
else if (type == -1)
{
btnOK.Enabled = false;
MessageBox.Show("温馨提示\n\n 连接服务器失败,请重新连接... ","消息",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
else
{
this.Invoke(new SuccessHandler(ConnectedSuccess),new object[]{result,type});
} } private void ConnectServer()
{
// string conStr =@"Provider=SQLOLEDB.1;User ID=sa;Password=mysql;Initial Catalog=DB_Rental;Data Source=tcp:192.168.0.154\server2005,1433;";
string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DB_RENTAL.mdb;Persist Security Info=False";
try
{
int type = UpdateUtility.ConnectServer(conStr);
ConnectedSuccess("\n服务器连接成功!!",type);
}
catch
{
ConnectedSuccess("\n连接服务器失败!!",-1);
}
}
private void btnConnect_Click(object sender, System.EventArgs e)
{ //连接服务器
ConnectedHandler con = new ConnectedHandler(ConnectServer);
con.BeginInvoke(null,null);
} }
}