Can't figure out problem

  • Thread starter Thread starter James Park
  • Start date Start date
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::Delete(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::Downloader(BackgroundWorker^ bw) ***
myBackgroundWorker = bw;

*** Downloader::OnDownloadFileCompleted ***
myBackgroundWorker->ReportProgress(50);
 
Thanks, that helped me track down the problem. I used a relative Uri for my
test on the DownloadFileAsync method without setting the BaseAddress
property for the WebClient. But why doesn't a try/catch block around that
call catch that exception?
 
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.

You can configure debugger to break on exceptions (Debug/Exception). Say C++
and CLR exceptions. This way you will now where InvalidOperationException
comes from.
 
Thanks, that helped me track down the problem. I used a relative Uri for my
test on the DownloadFileAsync method without setting the BaseAddress
property for the WebClient. But why doesn't a try/catch block around that
call catch that exception?

Don't know if it is exactly the answer to your question, but
exceptions can only be caught in the thread from which they originate.
(For example, ActiveMovie exceptions cannot usually be caught, since
AM usually starts multiple threads.)
 
Ah, that makes sense. Thanks.
Severian said:
Don't know if it is exactly the answer to your question, but
exceptions can only be caught in the thread from which they originate.
(For example, ActiveMovie exceptions cannot usually be caught, since
AM usually starts multiple threads.)
 
Back
Top