如何使用safemode将我的代码转换为C#多线程 [英] how to convert my code into C# multithreading with safemode

查看:83
本文介绍了如何使用safemode将我的代码转换为C#多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是C#开发人员,我正在学习C#.net中的多线程概念。我写了一个小程序,根据用户输入生成/复制单个图像到n次(n份)。



我想在代码下面制作在多线程中运行。请帮帮忙,我怎样才能在多线程中制作以下程序。



我需要通过复制大小为512 KB的单个图像来生成100,000个文件



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

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++;
                }
    }
    }







谢谢

Ommi




Thank you
Ommi

推荐答案

最简单的方法是用 for 循环=http://msdn.microsoft.com/en-us/library/dd460713(v=vs.110).aspx> Parrallel.For [ ^ ]。

并行class非常简单易用: http://msdn.microsoft.com/en -us / library / system.threading.tasks.parallel.aspx [ ^ ]



但在你的情况下,我怀疑并行性不会给你带来什么速度提升,因为你的系统几乎肯定会非常非常快地成为IO绑定 - 很久以前显着的多处理优势变得明显。
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.


希望这个链接可以帮助你应用parallel.foreach()以实现多线程



http://msdn.microsoft.com/en-us/library /dd460720(v=vs.110).aspx [ ^ ]
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[^]


这篇关于如何使用safemode将我的代码转换为C#多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆