Problems Parallel For with StreamWriter

Hello,

Implemented Parallel.For as a test on a project I'm developing on a Windows Services.

I'm having some problems because the service scans a SQL database table and returns aproximadamento some 2 million records, then it performs some actions and finally it writes the result of operation fied in a text file.

Looking at the text file I realized that it works well but some records are compromised because some threads try to write the file while the value of the record is wrong.

How can I do to allow a result to be written on the bench only after a thread may have finished writing?

It is worth observing that I'm using a server with 16 processing cores.

Part code:

FileStream fs = new FileStream("C:\\Logs\\001_log.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
StreamWriter fp = new StreamWriter(fs);
Parallel.For(0, table.Rows.Count, (k) =>
{
    try
    {
        Int32 iReturn = ProcRegister((Int32)table.Rows[0][0]);
        fp.WriteLine(iReturn);
        fp.Flush();
    }
    catch{}
    finally{}
});

fp.Close();
fp.Dispose();
fs.Dispose();
fs.Close();




Part of the log file with problem:

...
985217
991848
985680
988684
989989
983342
7367  983342 <---
7367  983342 <---
7367         <---
984540
986244
986016
984964
991237
...

Regards,
GrupoLayerdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrupoLayerdevAuthor Commented:
I'm solve the problem.


FileStream fs = new FileStream("C:\\Logs\\001_log.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

Object locker = new Object()

StreamWriter fp = new StreamWriter(fs);

Parallel.For(0, table.Rows.Count, (k) =>
{
    try
    {
        Int32 iReturn = ProcRegister((Int32)table.Rows[0][0]);
        lock (locker)
        {
            fp.WriteLine(iReturn);
            fp.Flush();
        }
    }
    catch{}
    finally{}
});

fp.Close();
fp.Dispose();
fs.Dispose();
fs.Close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.