Solved

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

Posted on 2011-02-19
6
246 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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 …

786 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