I have two processes where the First process sometimes is writing to a file and the second process is reading this file.

For the second process I wonder if it is possible to only read the file if the First process does not use the file.

How would it be possible to check so the file aren´t in use ?

String^ SymbolPath = "C:\\testFile.txt";

Recommended Answers

All 4 Replies

Thanks, that could been a good idéa but the problem is that there are 2 softwares that are reading from the same file so it is not possible to set a flag within the program in that way.

I think I am looking for a way to detect if the file itself aren´t in use by another process.

For this scenario, I think I simply need to detect if the file is closed.

May be this can help you - http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=144

Take a look at this page:
http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx

Assuming that the other processes you mentioned are programs that you've written and as long as I understood the above page correctly, if you set the code for all of your programs/processes to open data files using this syntax:

FileStream^ fs = File::Open( path, FileMode::Open, FileAccess::Write, FileShare::None );

or:

FileStream^ fs = File::Open( path, FileMode::Open, FileAccess::Read, FileShare::None );

That should stop other processes from being able to open the same file. I imagine there will be an exception thrown by any attempt to open a locked file, so if your processes are also set up to catch any relevant exceptions that might also help things along!

So any time your programs need to open a file for read/write, try opening with the above code. If an exception is thrown, perhaps set up some kind of timer for a few milliseconds and then try opening the file again.
If all of your processes are doing this, then I guess you can be sure that the only time your processes will be able to use the file is when none of the others are using it!

Not sure if that's any help, I've not done any managed C++ in donkeys years. But from an initial glance that's how things look to me!
Cheers for now,
Jas.

Thank you, yes, I have written these 2 programs. It seems to be a good idéa. So I think the below code will work as the scenario to ´try´ open the file. If the catch event occurs that will meen that the File is in use.
It sounds right. Thanks !

So I will put the code in a:

try
{
    //open file
   //Continue process the file and other code here
}
catch(Exception^ ex)
{
    //Failure to open file
}

Take a look at this page:
http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx

Assuming that the other processes you mentioned are programs that you've written and as long as I understood the above page correctly, if you set the code for all of your programs/processes to open data files using this syntax:

FileStream^ fs = File::Open( path, FileMode::Open, FileAccess::Write, FileShare::None );

or:

FileStream^ fs = File::Open( path, FileMode::Open, FileAccess::Read, FileShare::None );

That should stop other processes from being able to open the same file. I imagine there will be an exception thrown by any attempt to open a locked file, so if your processes are also set up to catch any relevant exceptions that might also help things along!

So any time your programs need to open a file for read/write, try opening with the above code. If an exception is thrown, perhaps set up some kind of timer for a few milliseconds and then try opening the file again.
If all of your processes are doing this, then I guess you can be sure that the only time your processes will be able to use the file is when none of the others are using it!

Not sure if that's any help, I've not done any managed C++ in donkeys years. But from an initial glance that's how things look to me!
Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.