Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forms application "Not Responding" when copying large files?

I've got a file organizer application I'm working on. The files are regularly 500MB to 2GB. Everything works fine, but it is extremely annoying that the application "Stops Responding." What I'd like to do is a byte by byte or meg by meg copy with some Application.DoEvents() in there after each read/write action. Something along these lines, I don't know what the actual classes to use would be so I'm just going to make some stuff up :)

private void CopyFile(string inFilename, string outFilename)
{
    FileReader inReader(inFilename);
    FileWriter outWriter(outFilename, FileMode.OpenOrCreate);

    byte theByte;
    while (theByte = inReader.ReadByte())
    {
        outWriter.WriteByte(theByte, WriteMode.Append);
        UpdateProgressBar();
        Application.DoEvents();
    }

    inReader.CloseFile();
    outWriter.CloseFile();
}

I know this seems like it should be a simple thing to do but I for the life of me can't seem to find any kind of example for doing this without using direct API calls or whatever. I've got to be missing something here so if anyone could get me on the right track here I'd really appreciate it.

Thanks in advance!

like image 942
Aaron Avatar asked Dec 20 '25 03:12

Aaron


1 Answers

You should use a BackgroundWorker on your form to do the copying. It will allow the file copies to be done on a separate thread and let your UI be responsive. There is some added complexity, but the BackgroundWorker takes care of a lot of the plumbing for you. But, there are plenty of examples of what you want to do.

like image 181
JP Alioto Avatar answered Dec 22 '25 17:12

JP Alioto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!