Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:57:26+00:00 2026-05-27T12:57:26+00:00

I’m building a console application that have to process a bunch of document. To

  • 0

I’m building a console application that have to process a bunch of document.

To stay simple, the process is :

  1. for each year between X and Y, query the DB to get a list of document reference to process
  2. for each of this reference, process a local file

The process method is, I think, independent and should be parallelized as soon as input args are different :

private static bool ProcessDocument(
        DocumentsDataset.DocumentsRow d,
        string langCode
)
{         
        try
        {                           
            var htmFileName = d.UniqueDocRef.Trim() + langCode + ".htm";
            var htmFullPath = Path.Combine("x:\path", htmFileName;

            missingHtmlFile = !File.Exists(htmFullPath);

            if (!missingHtmlFile)
            {
                var html = File.ReadAllText(htmFullPath);

                // ProcessHtml is quite long : it use a regex search for a list of reference
                // which are other documents, then sends the result to a custom WS

                ProcessHtml(ref html);

                File.WriteAllText(htmFullPath, html);

            }

            return true;             
        }
        catch (Exception exc)
        {
            Trace.TraceError("{0,8}Fail processing {1} : {2}","[FATAL]", d.UniqueDocRef, exc.ToString());
            return false;
        }
    }

In order to enumerate my document, I have this method :

    private static IEnumerable<DocumentsDataset.DocumentsRow> EnumerateDocuments()
    {           
        return Enumerable.Range(1990, 2020 - 1990).AsParallel().SelectMany(year => {
            return Document.FindAll((short)year).Documents;
        });
    }

Document is a business class that wrap the retrieval of documents. The output of this method is a typed dataset (I’m returning the Documents table). The method is waiting for a year and I’m sure a document can’t be returned by more than one year (year is part of the key actually).

Note the use of AsParallel() here, but I never got issue with this one.

Now, my main method is :

        var documents = EnumerateDocuments();

        var result = documents.Select(d => {
            bool success = true;
            foreach (var langCode in new string[] { "-e","-f" })
            {
                success &= ProcessDocument(d, langCode);
            }
            return new { 
                d.UniqueDocRef,
                success
            };
        });
        using (var sw = File.CreateText("summary.csv"))
        {
            sw.WriteLine("Level;UniqueDocRef");
            foreach (var item in result)
            {
                string level;
                if (!item.success) level = "[ERROR]";
                else level = "[OK]";

                sw.WriteLine(
                    "{0};{1}",
                    level,
                    item.UniqueDocRef
                    );
                //sw.WriteLine(item);
            }
        }

This method works as expected under this form. However, if I replace

        var documents = EnumerateDocuments();

by

        var documents = EnumerateDocuments().AsParrallel();

It stops to work, and I don’t understand why.

The error appears exactly here (in my process method):

File.WriteAllText(htmFullPath, html);

It tells me that the file is already opened by another program.

I don’t understand what can cause my program not to works as expected. As my documents variable is an IEnumerable returning unique values, why my process method is breaking ?

thx for advises

[Edit] Code for retrieving document :

    /// <summary>
    /// Get all documents in data store
    /// </summary>
    public static DocumentsDS FindAll(short? year)
    {
        Database db = DatabaseFactory.CreateDatabase(connStringName); // MS Entlib

        DbCommand cm = db.GetStoredProcCommand("Document_Select");
        if (year.HasValue) db.AddInParameter(cm, "Year", DbType.Int16, year.Value);

        string[] tableNames = { "Documents", "Years" };
        DocumentsDS ds = new DocumentsDS();
        db.LoadDataSet(cm, ds, tableNames);

        return ds;
    }

[Edit2] Possible source of my issue, thanks to mquander. If I wrote :

        var test = EnumerateDocuments().AsParallel().Select(d => d.UniqueDocRef);

        var testGr = test.GroupBy(d => d).Select(d => new { d.Key, Count = d.Count() }).Where(c=>c.Count>1);

        var testLst = testGr.ToList();

        Console.WriteLine(testLst.Where(x => x.Count == 1).Count());
        Console.WriteLine(testLst.Where(x => x.Count > 1).Count());

I get this result :

0
1758

Removing the AsParallel returns the same output.

Conclusion : my EnumerateDocuments have something wrong and returns twice each documents.

Have to dive here I think

This is probably my source enumeration in cause

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:57:27+00:00Added an answer on May 27, 2026 at 12:57 pm

    Is Document.FindAll((short)year).Documents threadsafe? Because the difference between the first and the second version is that in the second (broken) version, this call is running multiple times concurrently. That could plausibly be the cause of the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.