在后台运算的时候,想加个有进度条的窗体在前面循环滚动,可问题是后台的运算占据了系统资源,导致机器很卡,像死机一样,根本看不到进度条的效果,请问应该如何解决挨啊?

解决方案 »

  1.   

    Application.DoEvents();应该加在哪里啊?
      

  2.   


    for (int i=0;i<50000;i++)
    {
        处理函数();    progressBar1.Value = i;    Application.DoEvents();
        
    }
      

  3.   

    注,上面的例子里progressBar1.Minimum = 0;progressBar1.Maximum = 50000;
      

  4.   

    我的一段多线程代码,如要通用的进度条源码,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);
    } }
    }