Link to home
Start Free TrialLog in
Avatar of accucom
accucom

asked on

Multithreading in C#

I have a c# program which I have running as a service that awaits for specific files and processes them, and while it works as desired for one file at a time which can take up to 5 minutes each, I will now have a situation where more than one file may need to be processed at the same time, so I need to add an ability to perform on multiple files at the same time.  Here's a snippet of my code in a program that's over 2500 lines long:

            while (true)
            {
                string[] files = System.IO.Directory.GetFiles(GlobalVars.REQpath, "*.req", SearchOption.TopDirectoryOnly);
//              acquire all *.req files in folder
                for (int fcnt = 0; fcnt < files.Length; fcnt++)  
                {
                    doRequestFile(files[fcnt]);
                }
                Thread.Sleep(1000);
            }

How difficult or what's involved in order to make it perform the "doRequestFile" call in multithreading mode?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

ASKER CERTIFIED SOLUTION
Avatar of Beartlaoi
Beartlaoi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now, do you need to know when all those threads are complete?  If so there is stuff to add.
Avatar of accucom
accucom

ASKER

I have global variables and references to external web services and essentially reading and writing to/from different xml files and sometimes converting base-64 text to image files while also writing out a log file, and don't necessarily need to know if all threads are complete as I would expect them each to terminate if done at least.  This is essentially a console application that runs behind the scenes on a dedicated workstation that has certificate credentials.

I'm mainly using a Filemaker search application as a front-end from client workstations that sends out the individual file from each client and then waits for a specific answer file to become available before it proceeds.
If all the threads are writing to the same log file then you might want to serialize access to that.  If you have a global variable that stores the name of the log file or something like that just "lock" it when using it.  Look up the lock keyword in online help.
If more than one file will use the same web service connection instance then lock that object as well.
Avatar of accucom

ASKER

Thanks for suggested code Bearrtloai as I'll give it a try as I hope it's that simple.  Hopefully it does the trick as I don't believe I'll need to lock the log file unless I have fears of there being strange clashes.
Avatar of accucom

ASKER

Hmmm, I'm not sure what I'm doing wrong here as the ThreadPool.QueueUserWorkItem line doesn't act like it's doing anything upon testing.  Here's a snippet of what I currently have:

           WaitCallback wcbdoRequestFile = new WaitCallback(doRequestFileO);
            while (true)
            {
                string[] files = System.IO.Directory.GetFiles(GlobalVars.REQpath, "*.req", SearchOption.TopDirectoryOnly);
//              acquire all *.req files in folder
                for (int fcnt = 0; fcnt < files.Length; fcnt++)  
                {
                    string rqfile = files[fcnt].Replace(".req", ".re0");
                    System.IO.File.Delete(rqfile);
                    System.IO.File.Copy(files[fcnt], rqfile);
                    System.IO.File.Delete(files[fcnt]);

                    //doRequestFile(rqfile);
                    ThreadPool.QueueUserWorkItem(wcbdoRequestFile, rqfile);
                }
                //Thread.Sleep(1000);
            }
        }

        static void doRequestFileO(object o)
        {
            doRequestFile((string)o);
        }

        static void doRequestFile(string ReqFile)
        {
Avatar of accucom

ASKER

Edit: nevermind, just seems a bit slower, but now it seems to work.  Strange that I couldn't step through this in my debugging mode.

I'll test this out a bit more, hopefully it works as desired.
Avatar of accucom

ASKER

So far it works in further tests, shall await when I'm able to get 2 or more users from different workstations to try it out as I now need to post another question on a completely different topic.