gmalenko
gmalenko

Reputation: 162

Multi Threading and Task issue

This is the first time I'm attempting multiple threads in a project so bear with me. The idea is this. I have a bunch of documents I need converted to pdf. I am using itextsharp to do the conversion for me. When run iteratively, the program runs fine but slow.

I have a list of items that need to be converted. I take that list and split it into 2 lists.

                for (int i = 0; i < essaylist.Count / 2; i++)
                {                        
                    frontessay.Add(essaylist[i]);
                    try
                    {
                        backessay.Add(essaylist[essaylist.Count - i]);
                    }
                    catch(Exception e)
                    {
                    }
                }
                if (essaylist.Count > 1)
                {
                    var essay1 = new Essay();
                    Thread t1 = new Thread(() => essay1.StartThread(frontessay));
                    Thread t2 = new Thread(() => essay1.StartThread(backessay));

                    t1.Start();
                    t2.Start();

                    t1.Join();
                    t2.Join();
                }
                else
                {
                    var essay1 = new Essay();
                    essay1.GenerateEssays(essaylist[1]);
                }

I then create 2 threads that run this code

    public void StartThread(List<Essay> essaylist)
    {
        var essay = new Essay();
        List<System.Threading.Tasks.Task> tasklist = new List<System.Threading.Tasks.Task>();
        int threadcount = 7;
        Boolean threadcomplete = false;
        int counter = 0;
        for (int i = 0; i < essaylist.Count; i++)
        {               
            essay = essaylist[i];
            var task1 = System.Threading.Tasks.Task.Factory.StartNew(() => essay.GenerateEssays(essay));
            tasklist.Add(task1);
            counter++;
            if (tasklist.Count % threadcount == 0)
            {
                tasklist.ForEach(t => t.Wait());
                //counter = 0;
                tasklist = new List<System.Threading.Tasks.Task>();
                threadcomplete = true;
            }
            Thread.Sleep(100);
        }
        tasklist.ForEach(t => t.Wait());
        Thread.Sleep(100);
    }

For the majority of the files, the code runs as it should. However, for example I have 155 items that need to be convereted. When the program finishes and I look at the results I have 149 items instead of 155. It seems like the results are something like the total = list - threadcount. In this case its 7. Any ideas on how to correct this? Am I even doing threads/tasks correctly?

Also the essay.GenerateEssays code is the actual itextsharp that converts the info from the db to the actual pdf.

Upvotes: 0

Views: 382

Answers (1)

I4V
I4V

Reputation: 35353

How about using TPL. It seems that all your code can be replaced with this

Parallel.ForEach(essaylist, essay =>
{
    YourAction(essay);
});

Upvotes: 5

Related Questions