Yes, I could do that. But my gut feeling is that it won't duplicate my
current scenario exactly and won't repro the problem, and the result
would be no help at all. There's a lot more going on under a .NET app
and it seems related to .NET somehow.
Well, then at least reproduce it using .NET.
[...]
You may in fact be able to ask the question more generally (e.g. "I'm
waiting for the process to exit, but even after that happens, it still
seems to have the file locked briefly"), but of course as always they
may find the question more answerable if you provide a
concise-but-complete code example (which I'll note you haven't even
provided here
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
).
It's a large project. If I thought it'd be helpful at all, I'd be happy
to provide more code than I've posted already.
I went ahead and made an attempt. However, I'm unable to reproduce the
problem you're reporting. See below for the code I wrote. It's been
running for the last few minutes, and is up to about 3000 iterations
without any errors.
The fact that it's a large project is of no real consequence. You
shouldn't be posting the whole project anyway. You should be narrowing
down the problem until you have the smallest amount of code that still
reproduces the problem. And frankly, at this point, I really don't see
any way forward, at least not for me. It's not really possible for anyone
else to offer reliable advice without being able to reproduce the problem
themselves.
And of course, you have a perfectly reasonable work-around. If there's a
bug in your code causing the problem, it would be much better to find and
fix that instead. But if you're short on time, the work-around is easy,
and you can revisit the problem later.
Now up to 6000 iterations, by the way.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Still no errors.
Pete
Main program:
using System;
using System.IO;
using System.Diagnostics;
namespace TestProcessFileLock
{
class Program
{
static void Main(string[] args)
{
try
{
Random rnd = new Random();
int iter = 0;
while (true)
{
string strFilename = Path.GetTempFileName();
using (FileStream fs = new FileStream(strFilename,
FileMode.Truncate))
{
byte[] rgb = new byte[1024];
int cb = rnd.Next(100 * 1024, 500 * 1024);
while (cb > 0)
{
int cbWrite = Math.Min(rgb.Length, cb);
rnd.NextBytes(rgb);
fs.Write(rgb, 0, cbWrite);
cb -= cbWrite;
}
}
Process process = new Process();
process.StartInfo.FileName = "DummyProcess.exe";
process.StartInfo.Arguments = "\"" + strFilename +
"\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
Console.WriteLine("{0}: \"{1}\"", (++iter).ToString(),
strFilename);
process.Start();
string strOutput;
while ((strOutput = process.StandardOutput.ReadLine())
!= null)
{
Console.WriteLine("output: \"{0}\"", strOutput);
}
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.WriteLine("process exited with error");
Console.ReadLine();
}
File.Delete(strFilename);
}
}
catch (Exception exc)
{
Console.WriteLine("Error: \"" + exc.Message + "\"");
}
Console.ReadLine();
}
}
}
DummyProgram.exe (make sure a copy of this .exe is in the same directory
as the main program):
using System;
using System.IO;
namespace DummyProcess
{
class Program
{
static int Main(string[] args)
{
int ret = 0;
if (args.Length < 1)
{
Console.WriteLine("must provide a filename as the first
command-line argument");
ret = 1;
}
try
{
using (FileStream fs = new FileStream(args[0],
FileMode.Open))
{
byte[] rgb = new byte[1024];
Console.WriteLine("reading {0} bytes",
fs.Length.ToString());
while (fs.Read(rgb, 0, rgb.Length) > 0)
{
}
}
}
catch (Exception exc)
{
Console.WriteLine("Error: \"" + exc.Message + "\"");
ret = 1;
}
return ret;
}
}
}