2013-05-21 29 views
0

我试图使用this great project但由于我需要扫描许多映像,因此我需要花费很多时间来处理多线程。
但是,由于使图像实际处理的类使用Static methods并且正在操作Objectsref我不确定如何正确执行此操作。我从我的主线程中调用方法是:从C#中的线程调用静态方法

public static void ScanPage(ref System.Collections.ArrayList CodesRead, Bitmap bmp, int numscans, ScanDirection direction, BarcodeType types) 
{ 
    //added only the signature, actual class has over 1000 rows 
    //inside this function there are calls to other 
    //static functions that makes some image processing 
} 

我的问题是,如果它的安全使用,使用此功能是这样的:

List<string> filePaths = new List<string>(); 
     Parallel.For(0, filePaths.Count, a => 
       { 
        ArrayList al = new ArrayList(); 
        BarcodeImaging.ScanPage(ref al, ...); 
       }); 

我已经花了几个小时的调试,并大部分时间我得到的结果是正确的,但我确实遇到了几个我现在无法重现的错误。

编辑
我粘贴类的代码在这里:http://pastebin.com/UeE6qBHx

+2

没有分析方法本身没人能告诉你它是否是线程安全的! – Yahia

+1

“伟大的项目”仍使用过时的ArrayList类,这让我担心它的线程安全性。 –

+0

如果它使用全局变量(或者变量没有用作参数或函数内部),那么它是一个否定的。 –

回答

0

有没有办法告诉除非你知道,如果它存储在局部变量或字段的值(在静态类,而不是方法)。

所有本地变量都将罚款和实例每次调用,但字段不会。

一个极坏的榜样:

public static class TestClass 
{ 
    public static double Data; 
    public static string StringData = ""; 

    // Can, and will quite often, return wrong values. 
    // for example returning the result of f(8) instead of f(5) 
    // if Data is changed before StringData is calculated. 
    public static string ChangeStaticVariables(int x) 
    { 
     Data = Math.Sqrt(x) + Math.Sqrt(x); 
     StringData = Data.ToString("0.000"); 
     return StringData; 
    } 

    // Won't return the wrong values, as the variables 
    // can't be changed by other threads. 
    public static string NonStaticVariables(int x) 
    { 
     var tData = Math.Sqrt(x) + Math.Sqrt(x); 
     return Data.ToString("0.000"); 
    } 
} 
1

我敢肯定它是线程安全的。 有两个字段,它们是配置字段,不在类内部修改。 所以基本上这个类没有状态,所有的计算都没有副作用 (除非我没有看到非常模糊的东西)。

这里不需要参考修饰符,因为参考没有修改。