日期:2014-05-17  浏览次数:20810 次

使用backgroundWorker组件,为什么还会占用主线程,导致卡的现象?
直接贴代码
打断点看的时候,主线程执行完backgroundWorker1.RunWorkerAsync();后 断点出了这里-> } 就一直卡着界面,要等OldUpload运行完才有反应
C# code

        private void btnOldUpload_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }   
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
             OldUpload();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           if (e.Error != null)
                Console.WriteLine("error");
            else
            {
                MSG("上传结束!");
            }       
        }

       /**需要长时间执行的方法**/
        private void OldUpload()
        {
            int ucount = dgvUsers.Rows.Count;
            int mcount = dgvOldMachines.Rows.Count;
            for (int i = 0; i < mcount; i++)//
            {
                dgvOldMachines.EndEdit();

                DataGridViewCheckBoxCell ckoldmachine = (DataGridViewCheckBoxCell)dgvOldMachines.Rows[i].Cells["xuanold"];
                Boolean flag = Convert.ToBoolean(ckoldmachine.Value);
                if (flag == true)//datagridview 设备选中的信息                {
                    int iMachineNumber = Convert.ToInt32(dgvOldMachines.Rows[i].Cells[2].Value.ToString());
                    MachineInfo m = MachineInfoBLL.SelectMachineInfoByEqpID(iMachineNumber.ToString());

                    for (int j = 0; j < ucount; j++)
                    {
                        dgvUsers.EndEdit();
                        DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dgvUsers.Rows[j].Cells["Column1"];
                        Boolean flagm = Convert.ToBoolean(checkcell.Value);

                        if (flagm == true)//datagridview人员选中的信息
                        {
                            try
                            {
                                int idwEnrollNumber = Convert.ToInt32(dgvUsers.Rows[j].Cells[1].Value.ToString());
                                string sName = dgvUsers.Rows[j].Cells[2].Value.ToString();
                                if (Connect(axCZKEM1, m.Ip, m.Port, iMachineNumber.ToString(), Convert.ToInt32(m.Commpassword)))
                                {
                                   // MSG("连接成功!");
                                    int idwErrorCode = 0;
                                    
                                    int idwFingerIndex = 0;
                                    string sTmpData = "";
                                    int iPrivilege = 0;
                                    bool bEnabled = true;
                                    int iUpdateFlag = 1;
                                    string sPassword = "";
                                    int iFlag = 1;
                                   
                                    axCZKEM1.EnableDevice(iMachineNumber, false);
                                    int iLastEnrollNumber = 0;//判断是否已上传用户信息

                                    if (axCZKEM1.BeginBatchUpdate(iMachineNumber, iUpdateFlag))//创建批处理数据的内存空间
                                    {
                                        List<TempLate> tl = TempLateBLL.GetOldTempLateBySSN(idwEnrollNumber.ToString());//根据考勤号查询新机器指纹信息
                                        if (tl.Count != 0)
                                        {
                                            foreach (TempLate t in tl)
                                            {
                                                idwFingerIndex = t.Fingerid;
                                                sTmpData = t.Template;
                                                if (idwEnrollNumber != iLastEnrollNumber)//判断是否已上传用