Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

I am a C# developer, i am learning multi thread concepts in C#.net. I wrote a small program to generate/copy a single image to n time (n number of copies) based on the user inputs.

I want to make below code to run in multi thread. Please help out, how can i make below program in multithread.

Roughly i need to generate 100,000 files by copying a single image whose size is 512 KB

C#
if (txtImageGeneratorCount.Text.Length > 0)
        {
            if (File.Exists(txtImagesPath.Text))
            {
                string fileWithoutExtension = Path.GetFileNameWithoutExtension(txtImagesPath.Text);
                string fileExtension = Path.GetExtension(txtImagesPath.Text);

            if (!Directory.Exists(Utility.CreatedFileName))
                 Directory.CreateDirectory(Utility.CreatedFilePath);

                for (int i = 1; i <= Int32.Parse(txtImageGeneratorCount.Text.ToString()); i++)
                {
                    File.Copy(txtImagesPath.Text, Utility.CreatedFilePath + @"\" + fileWithoutExtension + "_" + countfile + fileExtension, true);
                    countfile++;
                }
    }
    }



Thank you
Ommi
Posted
Comments
Richard MacCutchan 2-Jun-14 5:05am    
I have deleted your duplicate of this question; please post questions once only.

Simplest way would be to replace your for loop with Parrallel.For[^] instead.
The Parallel class is pretty trivial to use: http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx[^]

But in your case, I suspect parallelism isn't going to give you major speed boosts, since your system is almost certain to become IO bound very, very quickly - long before any significant multi-processing advantages become noticeable.
 
Share this answer
 
Hope this link will help you to apply parallel.foreach()inorder to achieve Multithreading

http://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900