还是上午的那个问题,后来变了一下不行了。但那个问题已经结帖了,所以重发一贴请教。本人初学C#,比较菜,往高手多多指点:
上个问题就不贴了,地址在http://community.csdn.net/Expert/TopicView3.asp?id=5633807
现在改成:
private void bnCopyFilesSecret_Click(object sender, EventArgs e)
        {
            DialogResult drResult = fbdOpenFolder.ShowDialog();
            if (drResult == DialogResult.OK)
            {
                foreach (DataGridViewRow dgvSelectedRow in dgvSecretResults.SelectedRows)
                {
                    alArrayPath.Add(dgvSelectedRow.Cells[2].Value);
                    alArrayName.Add(dgvSelectedRow.Cells[1].Value);
                }
                Thread tdThread = new Thread(new ThreadStart(fnCopyFiles));
                tdThread.ApartmentState = ApartmentState.STA;
                tdThread.Start();
            }
        }        private void fnCopyFiles()
        {
            CFileOperation.fnCopyFiles(alArrayPath, alArrayName,fbdOpenFolder.SelectedPath,lbProcessInfoSecret);        }
将下面那些代码封装成了一个CFileOperate类,结构如下:
public void fnCopyFiles(ArrayList alPath, ArrayList alName, string szDestinationPath, Label lbLabelProcessShow)
        {            for (int i = 0; i < alPath.Count; i++)
            {
                if (alPath[i] != null)
                {
                    if (File.Exists(alPath[i].ToString().Remove(0, 5)))
                    {
                        try
                        {
                            File.Copy(alPath[i].ToString().Remove(0, 5), szDestinationPath + "\\" + alName[i].ToString());
                        }
                        catch
                        {
                            //异常有待处理
                        }
                        fnSetLabelText(lbLabelProcessShow, "正在复制:" + alName[i].ToString());                    }
                }
            }
            MessageBox.Show("复制成功!");
            
        }
        private void fnSetLabelText(Label lbLabelProcessShow, string szLabelTextShow)
        {
            if (lbLabelProcessShow.InvokeRequired)
            {
                dSetTextCallback dInsertWords = new dSetTextCallback(fnSetLabelText);
                lbLabelProcessShow.Invoke(dInsertWords, new object[] { szLabelTextShow });
                
            }
            else
            {
                lbLabelProcessShow.Text = szLabelTextShow;
            }
        }
        delegate void dSetTextCallback(Label lbLabelProcessShow, string szLabelTextShow);
主要就是改的这里:
if (lbLabelProcessShow.InvokeRequired)
            {
                dSetTextCallback dInsertWords = new dSetTextCallback(fnSetLabelText);
                lbLabelProcessShow.Invoke(dInsertWords, new object[] { szLabelTextShow });
                
            }
前面是用的this,但是控件传到这边来用就不行了。关于这块我不是太熟,希望大哥们讲讲这种消息的机制,再帮我修改下程序。

解决方案 »

  1.   

    lbLabelProcessShow.Invoke(dInsertWords, new object[] { szLabelTextShow });
    就是这句报错:参数计数不匹配。
      

  2.   

    lbLabelProcessShow.Invoke(dInsertWords, new object[] { szLabelTextShow });该成:lbLabelProcessShow.Invoke(dInsertWords, new object[] { lbLabelProcessShow, szLabelTextShow });
      

  3.   

    delegate void dSetTextCallback(Label lbLabelProcessShow, string szLabelTextShow);
    要两个参数: lbLabelProcessShow 和 szLabelTextShow.可是你的代码里只给了一个szLabelTextShow :
    lbLabelProcessShow.Invoke(dInsertWords, new object[] { szLabelTextShow });