Solved

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

Posted on 2011-02-19
6
242 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.
Comment Utility

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.
Comment Utility
You can also use:

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

Author Comment

by:fantasylan
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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
Comment Utility
Hi wdosanjos

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

Expert Comment

by:wdosanjos
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now