Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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# 在C中从线程调用静态方法#_C#_Multithreading - Fatal编程技术网

C# 在C中从线程调用静态方法#

C# 在C中从线程调用静态方法#,c#,multithreading,C#,Multithreading,我正在尝试使用,但由于我需要扫描许多图像,因此该过程需要花费大量时间,因此我考虑使用多线程技术。 但是,由于实际处理图像的类使用静态方法,并且通过ref操作对象,我不确定如何正确操作。我从主线程调用的方法是: public static void ScanPage(ref System.Collections.ArrayList CodesRead, Bitmap bmp, int numscans, ScanDirection direction, BarcodeType types) {

我正在尝试使用,但由于我需要扫描许多图像,因此该过程需要花费大量时间,因此我考虑使用多线程技术。
但是,由于实际处理图像的类使用
静态方法
,并且通过
ref
操作
对象
,我不确定如何正确操作。我从主线程调用的方法是:

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, ...);
                });
List filepath=new List();
Parallel.For(0,filepath.Count,a=>
{
ArrayList al=新的ArrayList();
条形码成像扫描页(参考文献等);
});
我花了几个小时调试它,大部分时间我得到的结果都是正确的,但我确实遇到了一些错误,现在我似乎无法重现

编辑

我将类的代码粘贴到这里:

除非您知道它是否将值存储在局部变量或字段中(在静态类中,而不是方法中),否则无法判断

所有局部变量都很好,每次调用都会实例化,但字段不会

一个非常糟糕的例子:

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");
    }
}

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


此处不需要Ref修饰符,因为引用未被修改。

如果不分析方法本身,没有人可以告诉您它是否是线程安全的!“伟大的项目”仍然使用过时的
ArrayList
类,这一事实让我担心它的线程安全性。如果它使用全局变量(或未用作参数或函数内部的变量),那么它就不是了。@JohnSaunders项目很旧,但工作做得很好,我把它换成List@the_lotus通过ref传递参数是否被视为全局变量?