Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Can't figure out problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James Park - 24 Jun 2005 20:51 GMT
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);
Vladimir Nesterovsky - 25 Jun 2005 07:42 GMT
> 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.

> 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?

Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com

James Park - 25 Jun 2005 08:14 GMT
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
[quoted text clipped - 12 lines]
>> class
>> (Downloader). What am I doing wrong here?
Severian [MVP] - 25 Jun 2005 09:21 GMT
>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.)

>>> 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
[quoted text clipped - 12 lines]
>>> class
>>> (Downloader). What am I doing wrong here?

--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
James Park - 25 Jun 2005 16:52 GMT
Ah, that makes sense. Thanks.

>>Thanks, that helped me track down the problem. I used a relative Uri for
>>my
[quoted text clipped - 31 lines]
> Microsoft MVP, Windows SDK
> Posting email address is real, but please post replies on the newsgroup.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.