J
James Park
I'm trying to delete off a 0-byte file that gets created after trying to
download from a bad URL, but I'm getting an IOException that says that it's
being used. I also noticed an InvalidOperationException before the
IOException in the debug output, but I can't seem to try/catch where that
comes up. After stripping off excess code from my .NET 2.0 Windows Forms
app, I have a button, a BackgroundWorker, and a WebClient-inheriting class
(Downloader). What am I doing wrong here?
My code looks like this:
*** Form1 class variables ***
bool myIsDownloading;
*** Form1::button1_Click ***
button1->Enabled = false;
backgroundWorker1->RunWorkerAsync();
*** Form1::backgroundWorker1_DoWork ***
Downloader^ dlr = gcnew Downloader(worker);
String^ filename = "C:\\test.txt";
dlr->DownloadFileAsync(gcnew Uri("crap", UriKind::Relative), filename);
myIsDownloading = true;
while (myIsDownloading)
{
Threading::Thread::Sleep(200);
}
try
{
File:elete(filename);
}
catch (IOException^ e)
{
// "The process cannot access the file 'C:\\test.txt'
// because it is being used by another process."
// Huh?
MessageBox::Show(e->Message);
}
worker->ReportProgress(100);
*** Form1::backgroundWorker1_ProgressChanged ***
if (e->ProgressPercentage == 50)
{
myIsDownloading = false;
}
else if (e->ProgressPercentage == 100)
{
button1->Enabled = true;
}
*** Downloader:ownloader(BackgroundWorker^ bw) ***
myBackgroundWorker = bw;
*** Downloader::OnDownloadFileCompleted ***
myBackgroundWorker->ReportProgress(50);
download from a bad URL, but I'm getting an IOException that says that it's
being used. I also noticed an InvalidOperationException before the
IOException in the debug output, but I can't seem to try/catch where that
comes up. After stripping off excess code from my .NET 2.0 Windows Forms
app, I have a button, a BackgroundWorker, and a WebClient-inheriting class
(Downloader). What am I doing wrong here?
My code looks like this:
*** Form1 class variables ***
bool myIsDownloading;
*** Form1::button1_Click ***
button1->Enabled = false;
backgroundWorker1->RunWorkerAsync();
*** Form1::backgroundWorker1_DoWork ***
Downloader^ dlr = gcnew Downloader(worker);
String^ filename = "C:\\test.txt";
dlr->DownloadFileAsync(gcnew Uri("crap", UriKind::Relative), filename);
myIsDownloading = true;
while (myIsDownloading)
{
Threading::Thread::Sleep(200);
}
try
{
File:elete(filename);
}
catch (IOException^ e)
{
// "The process cannot access the file 'C:\\test.txt'
// because it is being used by another process."
// Huh?
MessageBox::Show(e->Message);
}
worker->ReportProgress(100);
*** Form1::backgroundWorker1_ProgressChanged ***
if (e->ProgressPercentage == 50)
{
myIsDownloading = false;
}
else if (e->ProgressPercentage == 100)
{
button1->Enabled = true;
}
*** Downloader:ownloader(BackgroundWorker^ bw) ***
myBackgroundWorker = bw;
*** Downloader::OnDownloadFileCompleted ***
myBackgroundWorker->ReportProgress(50);