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?
 
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Bob LearnedCommented:
If the application does throw an exception, does it indicate file and line number info?
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.