处理多个txt文件时出错

时间:2013-05-10 22:02:29

标签: c# visual-studio-2008 large-data

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

Form1

我的程序能够从(源文件夹)中搜索所有子文件夹中的所有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)。

1 个答案:

答案 0 :(得分: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就是源。那个过程对于那个过程有什么看法,我不知道它对你的副本是无能为力的