0

我正在构建一个程序来搜索用户设置的文件夹(源文件夹)中的所有 .xml 并将所有这些文件复制到另一个文件夹(目标文件夹)。

表格1

我的程序能够从(源文件夹)中搜索所有子文件夹中的所有 XML,结果返回大约 5000 个放置在列表中的文件,该列表稍后由函数处理,但他只能处理 31 个文件,然后出现“没有响应”,调试器显示程序在执行中停留了很长时间。

这是我的代码:

按钮动作:

private void btnCopiarSalvar_Click(object sender, EventArgs e)
{
    foreach (string name in listFileNames)
    {
         if (readXML(name ))
         {
              tbArquivo.Text = name ; //Feedback textbox, tell the current filename
         }                    
    }
    pbStatus.Increment(50); 
    cbFinal.Checked = true; //Feedback checkBox, to tell user that the task is over.
}

函数 ReadXML

public bool readXML(string name)
{
    //foreach (string nome in listaArquivos) 
    //{  //I tried to the foreach inside, but nothing Works.

    try
    {
        string text = null;
        string readBuffer = File.ReadAllText(name);
        text = readBuffer.Aggregate(text, (current, b) => current + b);
        var encoding = new ASCIIEncoding();
        Byte[] textobytes = encoding.GetBytes(text);

        if (!File.Exists(destino))
        {
            string destinoComNomeArquivo = destino + "\\" + Path.GetFileName(nome);
            using (FileStream fs = File.Create(destinoComNomeArquivo))
            {
                foreach (byte textobyte in textobytes)
                {
                    fs.WriteByte(textobyte);
                    pbProcess.PerformStep();
                }
                Console.WriteLine("Arquivo gravado " + Path.GetFileName(nome));
            }
        }
        pbProcess.PerformStep();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    //}
    return true;
}

错误:检测到 ContextSwitchDeadlock。

尝试过的解决方案:禁用托管调试助手。

禁用 MDA 后,程序仍然只能读取 31 个文件(5k)。

4

1 回答 1

1

我建议的第一件事是......不要做那种文件复制!改为使用该File.Copy功能。

尝试使用从MSDN截取的这段代码:

void DoCopy(string path)
{
    var copytask = new Task(() =>
    {
        string destinoComNomeArquivo = @"C:\" + Path.GetFileName(path);
        DirectoryCopy(path, destinoComNomeArquivo, false);
    });
    copytask.Start();
}

private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
    DirectoryInfo dir = new DirectoryInfo(sourceDirName);
    DirectoryInfo[] dirs = dir.GetDirectories();

    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    if (!Directory.Exists(destDirName))
    {
        Directory.CreateDirectory(destDirName);
    }

    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo file in files)
    {
        string temppath = Path.Combine(destDirName, file.Name);
        file.CopyTo(temppath, false);                
    }

    var counter = 0;
    var maxcounter = files.Count();

    while (maxcounter < counter)
    {
        var item = files.ElementAt(counter).Name;
        WriteAsnc(item);
        counter++;
    }

    if (copySubDirs)
    {
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }
}

const int _maxwritingprocess = Environment.ProcessorCount;
int _currentwritingtasks;

void WriteAsnc(string filepath)
{
    _currentwritingtasks++;
    var task = Task.Factory.StartNew(() => 
    {
        XDocument doc = XDocument.Load(filepath);
        doc.Elements().First().Add(new XAttribute("Attribute Name","Attribute Value"));
        doc.Save(filepath);
        _currentwritingtasks--;
    });
    if(_currentwritingtasks == _maxwritingprocess)
        task.Wait();
    _currentwritingtasks--;
}

下一点ContextSwitchDeadlock是线程问题,我认为你pbProcess是源头。那个过程是做什么的?

于 2013-05-10T22:13:28.877 回答