Solved

How to generate threads simultaneously to write to a file in an exclusive manner

Posted on 2011-02-19
6
247 Views
Last Modified: 2012-05-11
Hello Experts,

I am writing a multi-threaded application which generates 10 threads simultaneously and then calls a method to write to a file in an exclusive manner i.e. One thread at a time. How do I do this?
Can I use ThreadPool to do this? If so, how do I go about implementing this?

Regards.
0
Comment
Question by:fantasylan
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:George K.
ID: 34934530

You can wait for another thread to end with Join method:
static void Main()
{
  Thread myt = new Thread (executecode);
myt.Start();
 myt.Join();
  Console.WriteLine ("thread myt is finished");
}
 
static void executecode()
{
 ... do someting here....;
}
0
 
LVL 13

Expert Comment

by:George K.
ID: 34934541
You can also use:

Thread.Sleep (500);                     //  500 milliseconds
0
 

Author Comment

by:fantasylan
ID: 34934547
Hi georgekl,

I am generating these threads simultaneously.
I think join is more reliable and faster than using sleep but this doesnt solve my problem,
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 34934634
The 'Thread' class should work fine and you need to synchronize your file writing method, so that only one thread writes to the file at any given time.  Something like this:

private File _myFile = new File();

public void WriteMyFile()
{
    lock (_myFile)
    {
        // Write to your file (only one thread at a time).
        // Other threads will wait on the 'lock' until the block is completed
     }
}

Open in new window

0
 

Author Comment

by:fantasylan
ID: 34934700
Hi wdosanjos

How do I indicate to the other waiting threads?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34934940
You don't need to indicate to them, they'll automatic wait on the 'lock' once they hit it and another thread is already in the 'lock' block.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question