Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用return语句并行化循环?_C#_Loops_Parallel Processing_Return - Fatal编程技术网

C# 如何使用return语句并行化循环?

C# 如何使用return语句并行化循环?,c#,loops,parallel-processing,return,C#,Loops,Parallel Processing,Return,我编写了一个位图扩展方法,该方法将在另一个位图中搜索“this”位图,如果找到,将返回找到它的点。该算法通过逐行搜索较大图像的行来工作。我希望将这些行拆分为与我的机器上的内核一样多的部分,并异步搜索它们。如果其中一个线程找到图像,则需要取消其他线程。这可以通过一个类似的方法来实现吗 public static Point LocateWithin(this Bitmap smaller, Bitmap larger) { if (smaller.Width > large

我编写了一个位图扩展方法,该方法将在另一个位图中搜索“this”位图,如果找到,将返回找到它的点。该算法通过逐行搜索较大图像的行来工作。我希望将这些行拆分为与我的机器上的内核一样多的部分,并异步搜索它们。如果其中一个线程找到图像,则需要取消其他线程。这可以通过一个类似的方法来实现吗

public static Point LocateWithin(this Bitmap smaller, Bitmap larger) {

        if (smaller.Width > larger.Width || smaller.Height > larger.Height)
            return new Point(-1, -1);

        Rectangle smallerRectangle = new Rectangle(0, 0, smaller.Width, smaller.Height);
        Rectangle largerRectangle = new Rectangle(0, 0, larger.Width, larger.Height);

        BitmapData smallerData = smaller.LockBits(smallerRectangle,
                                                  ImageLockMode.ReadOnly,
                                                  PixelFormat.Format32bppRgb);
        BitmapData largerData = larger.LockBits(largerRectangle,
                                                ImageLockMode.ReadOnly,
                                                PixelFormat.Format32bppRgb);

        try {
            IntPtr largerPtr = largerData.Scan0;
            IntPtr smallerPtr = smallerData.Scan0;

            int smallerStrideLessOne = smallerData.Stride - 4;
            int smallerHeightLessOne = smaller.Height - 1;
            int heightDiff = larger.Height - smaller.Height;
            int widthDiff = larger.Width - smaller.Width;

            for (int yl = 0; yl <= heightDiff; yl++) {
                for (int xl = 0; xl <= widthDiff; xl++) {

                    if (memcmp(smallerPtr, largerPtr, smallerData.Stride) == 0) {

                        IntPtr lPtr = largerPtr;
                        IntPtr sPtr = smallerPtr;

                        bool isMatch = true;
                        for (int ys = 0; ys < smallerHeightLessOne; ys++) {
                            lPtr += largerData.Stride;
                            sPtr += smallerData.Stride;
                            if (memcmp(sPtr, lPtr, smallerData.Stride) != 0) {
                                isMatch = false;
                                break;
                            }
                        }
                        if (isMatch)
                            return new Point(xl, yl);
                    }
                    largerPtr += 4;
                }
                largerPtr += smallerStrideLessOne;
            }
        }
        finally {
            smaller.UnlockBits(smallerData);
            larger.UnlockBits(largerData);
        }
        return new Point(-1, -1);
    }
public static Point locatein(此位图越小,位图越大){
如果(较小的.Width>较大的.Width | |较小的.Height>较大的.Height)
返回新点(-1,-1);
矩形smallerRectangle=新矩形(0,0,更小的.Width,更小的.Height);
矩形大矩形=新矩形(0,0,更大的.Width,更大的.Height);
BitmapData smallerData=更小的锁位(smallerRectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format32bppRgb);
BitmapData largerData=较大的锁位(较大矩形,
ImageLockMode.ReadOnly,
PixelFormat.Format32bppRgb);
试一试{
IntPtr largerPtr=largerData.Scan0;
IntPtr smallerPtr=smallerData.Scan0;
int smallerStrideLessOne=smallerData.Stride-4;
int smallerHeightLessOne=更小。高度-1;
int heightDiff=较大的.Height-较小的.Height;
int widthDiff=较大的.Width-较小的.Width;

对于(int yl=0;yl当你完成这项工作时,我很想知道你能获得多少性能。似乎考虑到这是如此的I/O密集型,你从主内存中读取图像会有一个瓶颈,我想知道有多少线程在这里为你买了东西……我不怀疑他们会为你买东西,只是非常好奇有多少难道不是一个简单的共享bool“hasBeenFound”足够了吗?请确保定期检查,以及在返回之前检查。感谢您的回复。我不认为跨越行分区的较小图像会成为问题,因为线程仍然可以访问较大图像中的所有行,即使它们仅迭代其中的一部分。检查共享hasBeenFound bool from all线程会很好地完成这项工作,谢谢你的建议。我会在做了更改后发布我的结果。