473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Help!!

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

Dec 19 '06 #1
13 1434
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.

--
Dave Sexton

"Lyon" <zl*****@gmail.comwrote in message
news:11*********************@48g2000cwx.googlegrou ps.com...
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

Dec 19 '06 #2
Hi,

By read-only object I mean a read-only field typed as object.

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eS**************@TK2MSFTNGP02.phx.gbl...
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.

--
Dave Sexton

"Lyon" <zl*****@gmail.comwrote in message
news:11*********************@48g2000cwx.googlegrou ps.com...
>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


Dec 19 '06 #3
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,

(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.

--
Dave Sexton

"Lyon" <zl*****@gmail.comwrote in message
news:11*********************@48g2000cwx.googlegrou ps.com...
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
Dec 19 '06 #4
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
--
http://www.sturmnet.org/blog
Dec 19 '06 #5
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
--
http://www.sturmnet.org/blog
Dec 19 '06 #6
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

"Lyon" <zl*****@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
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,

(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.

--
Dave Sexton

"Lyon" <zl*****@gmail.comwrote in message
news:11*********************@48g2000cwx.googlegrou ps.com...
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

Dec 19 '06 #7
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.
--
Ignacio Machin
machin AT laceupsolutions com

"Lyon" <zl*****@gmail.comwrote in message
news:11*********************@48g2000cwx.googlegrou ps.com...
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

Dec 19 '06 #8
Lyon <zl*****@gmail.comwrote:
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[i] = new Thread
(new ParameterizedThreadStart(Run));
threads[i].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);
}
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 19 '06 #9
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.

Dec 20 '06 #10

"Jon дµÀ£º
>
class Test
{
static void Main()
{
Thread[] threads = new Thread[20];
for (int i=0; i < threads.Length; i++)
{
threads[i] = new Thread
(new ParameterizedThreadStart(Run));
threads[i].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

Dec 20 '06 #11
Lyon <zl*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 20 '06 #12
But my program works successfully now:)
"Jon дµÀ£º
"
Lyon <zl*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 20 '06 #13
Lyon <zl*****@gmail.comwrote:
But my program works successfully now:)
Coincidence - unless you've put the Join in the wrong place, and
effectively serialised everything.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.