views:

56

answers:

1
+2  Q: 

Threading concept

Hi,

Can somebody help me on this:

private Thread workerThread;
private EventWaitHandle waitHandle;


            if (workerThread == null)
            {
                workerThread = new Thread(new ThreadStart(Work));
                workerThread.Start();
                //workerThread.Join(); 
            }
            else if (workerThread.ThreadState == ThreadState.WaitSleepJoin)
            {
                waitHandle.Set();
            }

    private void Work()
    {
        while (true)
        {
            string filepath = RetrieveFile();
            if (filepath != null)
                ProcessFile(filepath);
            else
                // If no files left to process then wait
                waitHandle.WaitOne();
        }
    }

    private void ProcessFile(string filepath)
    {
        XMLCreation myXML = new XMLCreation();
        myXML.WriteXml(filepath, XMLFullFilePath);
    }

    private string RetrieveFile()
    {
        if (workQueue.Count > 0)
            return workQueue.Dequeue();
        else
            return null;
    }

see this is how all this work.

i have a filewatcher event that fires only when new file is being add to that folder, now the problem is its a small part of bigger application and when the file watcher fires there is another process which is accessing that file and i get error like this file is being used by another process. so i have tried to implement through threading but with the above code only some files are being processed, but in the log i can see all the files are being processed. Is it the right way to do it or am i missing something in it

thanks in adv.

+3  A: 

You will have to use a mutex to control who is accessing the file and allow only one process at a time to work with that file at the very first time. If you think that there is the possibility that more than one thread will be waiting to work with the same file then you will have to implement a producer-consumer threading system with a queue.

Here is the best documentation about threads you can find in .NET:

http://www.albahari.com/threading/

despart
@despart Nice link :-)
Seb
Ya it is but that is not my question see i have a application that is part of big application now i have to find out whether other application is accessing the same file if it is then i have to wait for that file to be released and process that file after that. see i have a file watcher event that fires only when the file is being created so in that particular folder two processes are trying to access the same file so i have to wait for the other process to get completed and then process the file which are queued. DO I HAVE TO USE THREADS as i am not that much comfortable in that.
@user280154 If you think that other processes that you don't have the source code are accessing to the same file, then this has nothing to do with threads. You will have to try to perform your action and if an IOException is thrown and the file is locked by another process then retry the action after waiting some time.
despart
How to do that as i am unable to do as u told me to do so