Process exits when reading a file which is being written

L

Lonifasiko

Hi,

Using Process class I asynchronously launch an executable (black box
executable) file from my Windows application. I mean asynchronously
because I've got an EventHandler for "Exited" event. Therefore, when
process finishes, "Exited" event is raised.

This executable writes a long file for over 1-5 minutes, and I, from
my application must read that file while is being generated.

Therefore, after launching the executable calling "Start()" method, I
initialize a timer which tries to read from the file every 20 seconds
for example.

The result is that when trying to read for the first time, process
exits and I need the process to continue executing. I thought it was
because of reading the file simultaneously (file sharing violation),
but if I try just to show a message, occurs the same. Process exits
always.

Then, which would be the best way to read a file that is being
written? Maybe threads? A FileSystemWatcher? Is it possible?

Thanks very much in advance.
 
C

colin

Lonifasiko said:
Hi,

Using Process class I asynchronously launch an executable (black box
executable) file from my Windows application. I mean asynchronously
because I've got an EventHandler for "Exited" event. Therefore, when
process finishes, "Exited" event is raised.

This executable writes a long file for over 1-5 minutes, and I, from
my application must read that file while is being generated.

Therefore, after launching the executable calling "Start()" method, I
initialize a timer which tries to read from the file every 20 seconds
for example.

The result is that when trying to read for the first time, process
exits and I need the process to continue executing. I thought it was
because of reading the file simultaneously (file sharing violation),
but if I try just to show a message, occurs the same. Process exits
always.

Then, which would be the best way to read a file that is being
written? Maybe threads? A FileSystemWatcher? Is it possible?

Thanks very much in advance.

Im doing the same thing with streamwriter and streamreader from 2 different
applications,
with no problems you just have to make sure the writer opens it with
share=read.
although the two executables are luanched independantly.

In my app if the stream reader reaches EOF it just sleeps, and retries again
later.

I think you must have some other problem if reading cuases the writing app
to exit.

Colin =^.^=
 
K

Kevin Spencer

By default, the System.IO.File.Open method attempts to open a file
exclusively. You can use the overload which takes a parameter of a FileShare
enumerated value (File.Open Method (String, FileMode, FileAccess,
FileShare)) to open it differently, allowing other threads and process to
access the file. See
http://msdn2.microsoft.com/en-us/library/system.io.fileshare.aspx for the
complete FileShare enumeration documentation.

Note that, since the default is exclusive, BOTH threads must use the
overloaded method. The second thread must use something like the following,
if the file is already opened for writing:

FileStream fs = new FileStream("C:\foo.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
L

Lonifasiko

Hi,

As I mention in my first post, the executable I'm launching it's a
black box for me, that is, I don't know how opens the file for writing
(exclusive mode or not). To make my work harder, I'm sure executable
opens the file for writing in exclusive mode.

Then, when trying to read the file being written, I'm using the very
confortable File.ReadAllLines(filepath) method, which does not give me
any possibility of opening the file using FileShare and FileAccess
attributes.

Any more ideas?

Thanks very much.
 
L

Lonifasiko

It's me again,

Just one question: If the black box executable I use opens the file
for writing purposes in exclusive mode, do I have any possibility of
reading it while black box executable is writing inside?

If not, I should tell black box executable developer as soon as
possible....

Thanks very much.
 
C

colin

Lonifasiko said:
It's me again,

Just one question: If the black box executable I use opens the file
for writing purposes in exclusive mode, do I have any possibility of
reading it while black box executable is writing inside?

If not, I should tell black box executable developer as soon as
possible....

well thats the purpose of exclusive mode to stop other processes opening it,
when you open a streamreader you can open it with a string=path/filename
wich doesnt let u set share mode,
but you can also open it with
StreamReader sr=new StreamReader(new
FileStream(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite));

wich lets you set the share mode etc.

Colin =^.^=
 
L

Lonifasiko

Hi,

Thanks for the replies. I'm still having many problems.

I think problems come from executing the executable asynchronously.
After launching executable with "Start()", next thing I do is to check
if executable has generated file. If so, it should start reading the
file.

Yesterday tried starting a timer that checks the generated file each
30 seconds and also tried adding a FileSystemWatcher in order to see
when the generated file is created, or changes, but this two
approaches do not work; they only make the executable process to
terminate. I understand these processes interfere the work of
asynchronous Process class and that's why terminates.

I would really appreciate nay more help. Thanks very much in advance.
 
L

Lonifasiko

Hi again,

I wanted to sum up in this new post my last discoveries ;-)

If I launch the executable in asynchronous mode, without doing nothing
more, the output CSV file is generated without problems.

Then, I add a button to my form and inside "Click" event, I just check
if file has been generated. Generated or not, I write some text in a
textbox. All this also launching the process in asynchronous mode.
Well.....does not work! Incredible but true!

I've also noticed black box executable generates a temporary file and
when finished its execution, renames this temporary file with the
definitive filename (filename I check in my "Click" event). Anyway,
although inside my "Click" event will never see the file I'm looking
for, there should not be any problem in writing just "File does not
exist" in a simple textbox. Even this does not work!

I'm really desperated with this issue.

Maybe starting a thread that checks (and reads) the output file would
do it?

Thanks for your patience.
 
L

Lonifasiko

Forget to tell you that I've built a little example in which I launch
asynchronously an executable that writes a file inside an infinite
loop.

Then, in the "click" event of a button, I try to read using
StreamReader sr=new StreamReader(new
FileStream(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite));
as Colin said. This works!

I'm able to read the file being written!

What do you think? Black box executable related?

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top