Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Threads trying to access same files

Posted on 2004-09-10
5
Medium Priority
?
179 Views
Last Modified: 2010-04-15
Hello,
I am using Threadpool to create mutliple threads each dealing with audio files. In some cases, there are 2 or 3 threads trying to access the same input file and then the same output files. Only the first one gets it and the others fail and throw exceptions. How can I make the 2nd or 3rd thread wait for the file till it's released by the first, so they can process the file too?
0
Comment
Question by:gvmdevelopment
5 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 500 total points
ID: 12030122
just check when you try to open the file (from the thread), if you can't then sleep for a while, then try again... instead of letting the exception kill the thread, you need to handle it and retry later
try
{
 //open file here
}
catch //if there's an exception, catch it here, then sleep for a while, and retry, put this in a while loop or something
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12031299
Lock the file, using the C# lock(expression) statement.

I'm sure you have a method that opens the file. I dunno what kind of file it is, but let's say it is an XML document. Without lock, it would look something like this:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filename);

xmlDoc.SelectSingleNode("//node").InnerText = "test";
xmlDoc.Save();

---------------

if you would call this 3 or 4 times and add it to the ThreadPool, erroneous results could occur if the last instance would overwrite the first. So, to avoid this, you can use the lock statement:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filename);

lock(xmlDoc)
{
   xmlDoc.SelectSingleNode("//node").InnerText = "test";
   xmlDoc.Save();
}

-------------------

This will lock the object and every other thread will wait till the object is ready to be used again. When the lock statement goes out of scope, it will be automatically available again.

This should work.

Razzie
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12031304
Ok it was an audio file, I should read better ;)
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12032803
Please use Mute to access it

suggest in method createthread you generate the thread and method readaudio is the method you read file.
then

code below:

class play audio
{
  private static Mutex mut = new Mutex();
    private const int numIterations = 1;
    private const int numThreads = 3;//number of threads you create

   private void createthread()
    {
        for(int i = 0; i < numThreads; i++)
        {
            Thread myThread = new Thread(new ThreadStart(ReadAudio));
            ReadAudio.Name = String.Format("ReadAudio", i + 1);
            ReadAudio.Start();
        }

        // The main thread exits, but the application continues to
        // run until all foreground threads have exited.
    }

    private static void ReadAudio()
    {
        mut.WaitOne();
        //read the file and doing what you want

        mut.ReleaseMutex();
    }
}
0
 
LVL 3

Expert Comment

by:a_goat
ID: 12044832
There's a neat trick for this:

string filename = @"c:\foo.wav";
lock(filename.ToLower())
{
    // Open file and do stuff here
}

Because of string-interning, two strings with the same value will always be the same object.  So you make the filename lower case (in case the cases are different), and lock the variable.  No thread can get past the lock until no other thread is in it.

It still won't cover the case where the file is in use by another process.  In that case you need to keep checking and trying
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month7 days, 16 hours left to enroll

824 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