Help!!

  • Thread starter Thread starter Lyon
  • Start date Start date
L

Lyon

Hi everyone:
I have 20 threads and each creats a file in the same directory,
respectively.
However, because the threads are not synchronized, and only some of the
20 threads
creat the file in the directory successfully.

How can I make sure that the 20 threads are able to creat the files in
the same directory successfully??

Thank you very much!

Lyon
 
Hi,

(Terrible name for a thread, BTW)

Are they trying to create the same file?

Well, you should think about synchronizing them anyway since you're
apparently experiencing multi-threading issues. Just lock some read-only
object.
 
Hi,

By read-only object I mean a read-only field typed as object.
 
Hello Lyon,
I have 20 threads and each creats a file in the same directory,
respectively.
However, because the threads are not synchronized, and only some of the
20 threads
creat the file in the directory successfully.

It sounds to me as if you're having these threads create the same file? If
they work on different files, I wouldn't think you should have problems...
on the other hand, if they are creating different files, it seems logical
that you'd have a conflict. I guess I don't quite see the point of your
question :-)
Any more details?
How can I make sure that the 20 threads are able to creat the files in
the same directory successfully??

To synchronize threads, use a lock. Jon's pages here explain threads in
..NET very well:

http://www.yoda.arachsys.com/csharp/threads/



Oliver Sturm
 
No, they don't creat the same file.
For example, thread named threadi create a file named filei, and all
the 20 created files
are in the same directory.

And I also considered to lock the critical source, and in this case, is
the directory.
Unfortunately, I find nothing in the MSDN about how to lock the
directroy.

Is there any way in the Visual 2005 to lock a directory?
"Dave Sexton дµÀ£º
"
 
Hello Lyon,
And I also considered to lock the critical source, and in this case, is
the directory.
Unfortunately, I find nothing in the MSDN about how to lock the
directroy.

I don't think you can lock a directory, but as yours is the only process
that needs to work with the lock, you can just as well use any other
internal object as the lock.

I think the important question is still: why do things fail for you in the
first place? Don't you get any error/exception information in those cases
where a file can't be created?


Oliver Sturm
 
Hi,

You don't need to lock a directory. As long as it's not read-only you
should be able to create your files without multi-threading issues as long
as they are all uniquely named.

I still think you are experiencing a multi-threading issue and a lock could
help, but as Oliver suggested you may want to provide the group with some
more details about the problem you are experiencing, such as exception
type/message and stack trace.

--
Dave Sexton

No, they don't creat the same file.
For example, thread named threadi create a file named filei, and all
the 20 created files
are in the same directory.

And I also considered to lock the critical source, and in this case, is
the directory.
Unfortunately, I find nothing in the MSDN about how to lock the
directroy.

Is there any way in the Visual 2005 to lock a directory?
"Dave Sexton дµÀ£º
"
 
Hi,

Can you post your code?

if all the thread are creating diffirent files you should have no problem at
all.

Your problem might be somewhere else.

You cannot lock the directory.
 
Lyon said:
No, they don't creat the same file.
For example, thread named threadi create a file named filei, and all
the 20 created files are in the same directory.

That should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's a program which works fine for me:

using System;
using System.IO;
using System.Threading;

class Test
{
static void Main()
{
Thread[] threads = new Thread[20];
for (int i=0; i < threads.Length; i++)
{
threads = new Thread
(new ParameterizedThreadStart(Run));
threads.Start(i);
}
foreach (Thread thread in threads)
{
thread.Join();
}
}

static void Run(object state)
{
byte[] buffer = new byte[1024];
for (int i=0; i < 100; i++)
{
string name = string.Format ("test{0}-{1}.txt", state, i);
using (FileStream fs = File.Create(name))
{
fs.Write(buffer, 0, buffer.Length);
}
}
}
}
 
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 ) дµÀ£º
"
 
"Jon дµÀ£º
class Test
{
static void Main()
{
Thread[] threads = new Thread[20];
for (int i=0; i < threads.Length; i++)
{
threads = new Thread
(new ParameterizedThreadStart(Run));
threads.Start(i);
}
foreach (Thread thread in threads)
{
thread.Join();////
}
}

Jon:
It maybe the thread.Join(); make your program works successfully, and
it also is what I need really.

Lyon
 
Lyon said:
It maybe the thread.Join(); make your program works successfully, and
it also is what I need really.

Very unlikely - that's just making the first thread wait for all the
rest to finish.
 
Lyon said:
But my program works successfully now:)

Coincidence - unless you've put the Join in the wrong place, and
effectively serialised everything.
 

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

Back
Top