Hi everyone:
Thank you very much for your suggestions.
Here is my code:
namespace MyDemon
{
class Program
{
internal static void Compile(object e)
{
string FilePathOfCSource =
@"E:\MinGWStudio\MinGW\bin\Test"+e.ToString()+".c" ;
Console.WriteLine("The ****************************{0}",e);
CCompiler myCompiler = new CCompiler();
myCompiler.Compile(FilePathOfCSource);
Console.WriteLine("In the {0} file: erro
msg:{1}",e.ToString(),myCompiler.ErrorMsg);
}
static void Main(string[] args)
{
for (int i = 0; i < 20; i++)
{
Thread myThread=new Thread (new
ParameterizedThreadStart(Program.Compile));
myThread.Start(i);
}
}
}
And the class CCompiler is defined as the following:
class CCompiler:CompilerBase
{
#region properties
internal override CompilerType CompilerType
{ get { return CompilerType.C; } }
internal override CompileStatus CompileStatus
{ get { return compileStatus;} }
internal override string ErrorMsg
{ get { return errorMsg;} }
internal override string OutputMsg
{ get { return null; } }
internal string OutExePath
{ get { return outExePath; } }
internal override string PathOftheCompiler
{ get { return PathOfAllCompilers.CompilerExcutePathC ; } }
#endregion
#region private members
private string filePathToCompile;
private string outExePath;
private string errorMsg;
private CompileStatus compileStatus;
#endregion
internal override void Compile(string filePathToCompile)
{
this.compileStatus = CompileStatus.Waiting;
this.filePathToCompile = filePathToCompile;
this.outExePath = filePathToCompile.Replace(".c", ".exe");
if (File.Exists(outExePath))
File.Delete(outExePath);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = this.PathOftheCompiler;
psi.Arguments = filePathToCompile + " -o " +
this.outExePath;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.ErrorDataReceived += new
DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.BeginErrorReadLine();
p.WaitForExit(2000);
if (!p.HasExited)
{
p.ErrorDataReceived -= new
DataReceivedEventHandler(p_ErrorDataReceived);
p.Kill();
this.compileStatus = CompileStatus.CompileError;
}
p.Close();
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs
e)
{
if (e.Data == null)
{
if (errorMsg == null)
this.compileStatus = CompileStatus.CompileOK;
else if (File.Exists(outExePath))
this.compileStatus =
CompileStatus.CompileOKWithWarning;
else
compileStatus = CompileStatus.CompileError;
}
else
{
string temp =
e.Data.Replace(filePathToCompile.Replace("\\", "/") + ":", "") +
"\r\n";
if(temp[0]<='9'&&temp[0]>='0')
{
temp = "In Line:" + temp;
}
this.errorMsg += temp;
}
}
}
internal abstract class CompilerBase
{
internal abstract CompilerType CompilerType { get;}
internal abstract CompileStatus CompileStatus { get;}
internal abstract string OutputMsg { get;}
internal abstract string ErrorMsg { get;}
internal abstract string PathOftheCompiler { get;}
internal abstract void Compile(string filePathToCompile);
}
Thus, I create 20 thread, and each thread creates a process(by starting
gcc.exe) to compile a c source file, and the created process in each
thread creates a exe file in the directory.
"Ignacio Machin ( .NET/ C# MVP ) дµÀ£º
"
Hi,
Can you post your code?
if all the thread are creating diffirent files you should have no problemat
all.
Your problem might be somewhere else.
You cannot lock the directory.