简体   繁体   English

多线程任务

[英]Multi-Threading a Task

Is there a way to multi-thread this method? 有没有一种方法可以对该方法进行多线程处理? The problem that I can see is that the only shared state is the cloning of the image source which takes up about half of the method time. 我可以看到的问题是,唯一的共享状态是图像源的克隆,这占用了大约一半的方法时间。 There are about 5 to 10 thousand recipients in the list. 列表中大约有5到1万个收件人。

I've tried a ConcurrentQueue with one action added clones and another TryDequeue and processing them but over 500 items it only saves 7 seconds. 我尝试了一个ConcurrentQueue,其中一个操作添加了克隆,另一个添加了TryDequeue并对其进行处理,但是超过500个项目仅节省7秒。 Is there a way to have the imgSrc roll back or undo the changes so it doesn't have to create a new clone? 有没有办法使imgSrc回滚或撤消更改,从而不必创建新的克隆?

    public static void CreateImages(string fileSrc, string pathDestination, IEnumerable<Recipient> recipients)
    {
        var sfCenter = new StringFormat();
        sfCenter.Alignment = StringAlignment.Center;
        sfCenter.LineAlignment = StringAlignment.Center;

        using (var imgSrc = new System.Drawing.Bitmap(fileSrc))
        {
            foreach (var rec in recipients)
            {
                using (var imgCopy = imgSrc.Clone() as Bitmap)
                using (var graphicImage = Graphics.FromImage(imgCopy))
                {
                    graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
                    graphicImage.DrawString(rec.Name, new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Rectangle(170, 105, 650, 50), sfCenter);
                    // plus other activity
                    var fileOut = pathDestination + rec.ID + ".jpg";
                    imgCopy.Save(fileOut, ImageFormat.Jpeg);
                }
            }
        }
    }

No better way found. 找不到更好的方法。 That looks as good as it gets. 看起来很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM