File IO Problem!!!

V

Vai2000

Hi All, I had posted a File IO Race condition question sometime back. With
all your advice now I am opening a file to check whether a valid File Handle
is returned or not? If not then I sleep for sometime and retry it.
Problem is when I re open the File I am missing a line? Any Clues?
bool IsFileInUse(string inputFileUNC)
{
bool bRet=true;
FileStream fs=null;
try
{
fs=new FileStream(inputFileUNC, FileMode.Open, FileAccess.Read,
FileShare.None);
bRet=false;
}
catch
{
// exception....
}
finally
{
if(fs!=null)
fs.Close();
}

return bRet;
}

Next time when the file handle is good I read the file like this
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

Can someone help with the problem?

TIA
 
J

Jared Parsons [MSFT]

It's difficult to tell what could be going wrong from the code you posted.
Could you post a more complete sample?

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
V

Vai2000

Ok, heres the code...its being executed inside a WinSvc

void Process(string inputFileUNC)
{
try
{
// AVOID LOCK CONDITIONS
retry:
if(IsFileInUse(inputFileUNC))
{
int sleepTime=ComputeSleepTime(inputFileUNC);
string msg=String.Format("Since file:{0} was in USE\nProcessing Thread
will Sleep for:{1} milliseconds",inputFileUNC,sleepTime);

Thread.Sleep(sleepTime);
goto retry;
}

ExecuteFile(inputFileUNC)
}
catch
{}

}

ExecuteFile(string fileIN)
{
string path = this.fileName;
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

try
{
string linedata=null;
//if carriage return read line by line
do
{
linedata = reader.ReadLine();

}while(linedata !=null);
}

Jared Parsons said:
It's difficult to tell what could be going wrong from the code you posted.
Could you post a more complete sample?

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Vai2000 said:
Hi All, I had posted a File IO Race condition question sometime back. With
all your advice now I am opening a file to check whether a valid File Handle
is returned or not? If not then I sleep for sometime and retry it.
Problem is when I re open the File I am missing a line? Any Clues?
bool IsFileInUse(string inputFileUNC)
{
bool bRet=true;
FileStream fs=null;
try
{
fs=new FileStream(inputFileUNC, FileMode.Open, FileAccess.Read,
FileShare.None);
bRet=false;
}
catch
{
// exception....
}
finally
{
if(fs!=null)
fs.Close();
}

return bRet;
}

Next time when the file handle is good I read the file like this
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

Can someone help with the problem?

TIA
 
S

Shakir Hussain

Vai,

couple of recomendations to try

1. Seek file pointer to begin before starting to read.
2. When you open the file deny sharing file to others by setting
FileShare.None.
3. Before opening the file get the File size to make sure, you have
something to read there.
FileInfo pInfo = new FileInfo(filename)
if(pInfo.Length > 0)
//open the file.

Shak.


Vai2000 said:
Ok, heres the code...its being executed inside a WinSvc

void Process(string inputFileUNC)
{
try
{
// AVOID LOCK CONDITIONS
retry:
if(IsFileInUse(inputFileUNC))
{
int sleepTime=ComputeSleepTime(inputFileUNC);
string msg=String.Format("Since file:{0} was in USE\nProcessing Thread
will Sleep for:{1} milliseconds",inputFileUNC,sleepTime);

Thread.Sleep(sleepTime);
goto retry;
}

ExecuteFile(inputFileUNC)
}
catch
{}

}

ExecuteFile(string fileIN)
{
string path = this.fileName;
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

try
{
string linedata=null;
//if carriage return read line by line
do
{
linedata = reader.ReadLine();

}while(linedata !=null);
}

Jared Parsons said:
It's difficult to tell what could be going wrong from the code you posted.
Could you post a more complete sample?

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Vai2000 said:
Hi All, I had posted a File IO Race condition question sometime back. With
all your advice now I am opening a file to check whether a valid File Handle
is returned or not? If not then I sleep for sometime and retry it.
Problem is when I re open the File I am missing a line? Any Clues?
bool IsFileInUse(string inputFileUNC)
{
bool bRet=true;
FileStream fs=null;
try
{
fs=new FileStream(inputFileUNC, FileMode.Open, FileAccess.Read,
FileShare.None);
bRet=false;
}
catch
{
// exception....
}
finally
{
if(fs!=null)
fs.Close();
}

return bRet;
}

Next time when the file handle is good I read the file like this
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

Can someone help with the problem?

TIA
 
V

Vai2000

Problem is if I run the same code as a Console App its fine...but when I try
to incorporate this in a WinSvc I run into problem.
Since my WinSvc is constantly monitoring a pick up directory....I think
there might be some issue.
Initial problem was that if I moved a large file via the File Explorer to
the pickup directory my WinSvc complained saying that File is in Use by some
another process. In order to solve that problem I wrote the code to check if
File is in Use or not? Now after that has been fixed I am running into the
problem of missing line count on the File, when I read it again....

TIA

Shakir Hussain said:
Vai,

couple of recomendations to try

1. Seek file pointer to begin before starting to read.
2. When you open the file deny sharing file to others by setting
FileShare.None.
3. Before opening the file get the File size to make sure, you have
something to read there.
FileInfo pInfo = new FileInfo(filename)
if(pInfo.Length > 0)
//open the file.

Shak.


Vai2000 said:
Ok, heres the code...its being executed inside a WinSvc

void Process(string inputFileUNC)
{
try
{
// AVOID LOCK CONDITIONS
retry:
if(IsFileInUse(inputFileUNC))
{
int sleepTime=ComputeSleepTime(inputFileUNC);
string msg=String.Format("Since file:{0} was in USE\nProcessing Thread
will Sleep for:{1} milliseconds",inputFileUNC,sleepTime);

Thread.Sleep(sleepTime);
goto retry;
}

ExecuteFile(inputFileUNC)
}
catch
{}

}

ExecuteFile(string fileIN)
{
string path = this.fileName;
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

try
{
string linedata=null;
//if carriage return read line by line
do
{
linedata = reader.ReadLine();

}while(linedata !=null);
}

Jared Parsons said:
It's difficult to tell what could be going wrong from the code you posted.
Could you post a more complete sample?

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Hi All, I had posted a File IO Race condition question sometime
back.
With
all your advice now I am opening a file to check whether a valid File
Handle
is returned or not? If not then I sleep for sometime and retry it.
Problem is when I re open the File I am missing a line? Any Clues?
bool IsFileInUse(string inputFileUNC)
{
bool bRet=true;
FileStream fs=null;
try
{
fs=new FileStream(inputFileUNC, FileMode.Open, FileAccess.Read,
FileShare.None);
bRet=false;
}
catch
{
// exception....
}
finally
{
if(fs!=null)
fs.Close();
}

return bRet;
}

Next time when the file handle is good I read the file like this
FileStream freader = new FileStream(this.fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader (freader);

Can someone help with the problem?

TIA
 
L

Lasse Vågsæther Karlsen

Ok, heres the code...its being executed inside a WinSvc

Couple of issues and questions:
1. you're "missing" a line, how do you verify this ? ie. do you count the
lines or do you output them or ... ?
2. in your execute method you don't close the StreamReader and FileStream
classes, unless you posted a partial example
3. please post a complete program, ie. something I can copy into a service
example, with a complete specification of what the problem is and how you
verify the problem
 

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