Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Is it possible for a program to "get out of time and step on itself"?

Sorry I don't have anything better to call it. Here is what is happening!

The application I am developing is to open a file, read the file's contents into memory, then close the file. Next do some operations on the file copy that is in the memory, then overwrite the original file because some of the data could have changed in the processing phase.

I have tested and what I would consider completed this piece of software, and it works beautifully running from the IDE. But when I install the compiled Setup1.MSI or Setup1.Exe file for my solution an exception is caught. This exception was not caught or thrown while the code was being executed in the IDE. Please help!

My code goes somthing like this:
 string inputFilePath = @"c:\test directory\testfile.dat";

 MyObject obj = new MyObject();
 obj = MyFileManager.Read( inputFilePath );

// Do the processing for the data ( process the MyObject obj )
   /*
    *    Long block of code executes to do all of the file processing.
    */

 // write the MyObject obj back to its file format
 try
 {
   MyFileManager.Write( inputFilePath );
 }
 catch( Exception ex)
 {
   MessageBox.Show(ex.ToString());
   // displays System.Io.IOException.
   // The process cannot access the file 'c:\test directory\testfile.dat'
   //  because it is being used by another process.
   //
   // While executting the installed version only !!!!!!!!!!!
}



The MyFileManager Object is a static class that is responsible for opening / writing the file and utilizes the System.IO.StreamReader and System.IO.StreamWriter Objects from the .NET libraries.


Like I say this works perfect running under the IDE, but not from the installed *.exe

The onlything that I can think of is the problem has to be a timing issue between running under the IDE and Just running in the Operating system?

Is that even possible?

Pleas help?

Thanks - Mike
Posted
Updated 25-Apr-11 12:50pm
v3
Comments
HimanshuJoshi 25-Apr-11 15:39pm    
Can you specify exactly what happens instead of saying something weired happens. I mean, does your file gets garbled, or the contents are not saved or anything. Give us specific details.
mike1989 25-Apr-11 16:23pm    
Sorry,

I have revised the question. By weird I mean that while I was test running the code using the IDE, All code executed perfect. But after installing the software and executing it directly from Windows with out the debugger, the software was throwing exceptions about the data file being locked.

1 solution

I think the problem might be in your file manager helper class. Are you opening the file and closing it on each call to the static functions, or are you keeping a file lock on the file by keeping it open? It depends on your requirements, but I would suggest closing the file after each read or write operation as long as your are reading and/or writing the entire contents of the file at once.

If you are using async file read write methods. I suppose there could be a timing issue, but not if they are synchonous.
 
Share this answer
 
Comments
mike1989 25-Apr-11 16:30pm    
I though the same thing, why would I only see this exception while running the installed version and during the times running in the IDE?


I have verified that I am closing the StreamReader object everytime I use it, like this

finally
{
if ( reader != null )
{
// make sure the file is closed
reader.Close();
reader.Dispose();
}
}
Wayne Stewart_ 25-Apr-11 16:59pm    
It could be a permissions issue. If you are running Visual Studio in an administrative mode, but when you run the installed app, it might be running with fewer priviledges. It might also be a problem if anything like System.Diagnostics.Debug.Equals is being used since it will not be called when the code is compiled with release configuration.

I still think the problem is probably with the file manager class. Would it be possible to see the entire code for that; the read and write functions at least.
mike1989 26-Apr-11 11:18am    
I wish I could. There are 1550 lines of code in this class, but only two places that deal with the opening and closing of the files. Most of the code is methods for decoding different blocks of data in the input file.

Do you know of a way I can upload it here?
Wayne Stewart_ 29-Apr-11 9:23am    
I don't know of a way to upload it here.

It definitely sounds like you have a race condition happening here. Running the project while debugging probably slows it down enough to work. If your app is multi threaded or you're using methods like BeginRead and BeginWrite, you need to make sure the previous operation is finished before starting the next.

I hope that helps, I can't suggest anything specific without seeing the code.
Henry Minute 25-Apr-11 18:56pm    
Just a small point, which will probably not solve your problem but is good practice.

Try to use the 'using' construct for your StreamReader, and any other IDisposable object, as in the example on this (http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx) page on MSDN. This will ensure that it gets closed.

Also when you ask a question about a problem where there is an exception being thrown it really helps us to help you, if you quote the entire exception message in the question. This narrows down the possible causes, leading to quicker, more accurate suggestions. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900