A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

I have a vb.net code which reads from a text file all network locations and then calculated the MD5 hash of those files and then further write hash values to output.txt file ... Program stops working after 500 lines and throw following error ... following is the code and error messages

CODE:
        Dim sNames As String() = {"A"}


        System.Threading.Tasks.Parallel.For(0, sNames.Count, Sub(i)

                                                                 For Each line As String In lines
                                                                     file.Write(MD5(line, 2))
                                                                     file.Write("@")
                                                                     file.WriteLine(line)
                                                                     ' i += 1

                                                                     ProgressBar1.Value += 1

                                                                 Next
                                                             End Sub)




        file.Close()

Open in new window



ERRORS:
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
The program '[11208] WindowsApplication1.vshost.exe' has exited with code 0 (0x0).

Open in new window

LVL 5
CalmSoulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
First chance exceptions are difficult to find a reason for, and are not thrown, and in my opinion they can be ignored.
0
CalmSoulAuthor Commented:
@Bob, but program stops how can I ignore and proceed in my code?
0
Bob LearnedCommented:
If the application does throw an exception, does it indicate file and line number info?
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

CalmSoulAuthor Commented:
it just prints "60" and nothing happens - no exception ... on output file it just stops writing ...
0
Jacques Bourgeois (James Burger)PresidentCommented:
What do you mean by "it just prints "60""? Where is it "printed"?

I told you, in another thread, that you can have problems writing to files when multitasking.

Here is what I think is happening.

You are starting a parallel job, which means that this will run in parallel with your application. But just after creating that parallel task, you close the file. Since both the parallel loop and the code that closes the file run in parallel, the file ends up being closed before the parallel task is complete. Since the parallel task tries to write in that file... BOOM.

Controlling parallel jobs requires a very good understanding of what you are doing, what is happening under the hood, and mostly of the logic that links both. You cannot debug it as you do for operations that runs sequentially. It's almost impossible to make it work if you do not understand all the possible problems. You have to read on the subject and understand what you read before you try anything about it.

That is why, in that other thread, it was suggested that you use a BackgroundWorker. It is the simplest way of dealing with multithreading in a Windows application, because it requires no knowledge other that what any programmer knows, that is calling a method and react to the events that can be triggered while the worker is doing its job. If I were you, I would still follow that path.
0
sarabandeCommented:
do you close the files afetr they were written? windows applications have a maximum of 512 file handles that can be opened same time. this maximum can be increased to 2048 (programmatically) but it is better to practice a proper closing of your files.

Sara
0
CalmSoulAuthor Commented:
sarabande:

Interesting, so where should I close them with in the loop after every line write?
0
sarabandeCommented:
where should I close them with in the loop after every line write?
you only have to close files if you opened a new (log) file for every record.

if not, the reason for your problems may be different.

beside of the exception, it may useful to open and close a logfile for each write. that allows you to peek into the logfile at any time and have a look at the most recent log outputs.

First chance exceptions are difficult to find a reason for, and are not thrown, and in my opinion they can be ignored.
a first chance exception 'FileNotFound' can be ignored, if you try to find out whether a new file already exists by opening the file read-only. then, an exception that the file doesn't exist obviously is an expected result that could be handled by opening the same file for write access, thus creating a new file. however, if you don't do such kind of exception handling, but are 'surprised' that a file where you expect that it exists is not existing, then you should try to find out where the exception was handled (otherwise it wouldn't be a 'first' chance exception) and find the corresponding try block which failed.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.