简体   繁体   English

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

[英]Invoking static methods from thread in C#

I'm trying to use this great project but since i need to scan many images the process takes a lot of time so i was thinking about multi-threading it. 我正在尝试使用这个很棒的项目,但是由于我需要扫描很多图像,因此该过程需要花费很多时间,因此我考虑对它进行多线程处理。
However, since the class that makes the actual processing of the images uses Static methods and is manipulating Objects by ref i'm not really sure how to do it right. 但是,由于进行图像实际处理的类使用Static methods并且通过ref来操纵Objects ,因此我不确定如何正确地进行操作。 the method that I call from my main Thread is: 我从主线程调用的方法是:

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
}

My question is if it's safe to use use this function like this: 我的问题是使用这种功能是否安全:

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

I've spent hours debugging it and most of the time the results i got were correct but i did encounter several errors which i now can't seem to reproduce. 我已经花了几个小时对其进行调试,并且大多数时候我得到的结果都是正确的,但是我确实遇到了几个错误,这些错误现在似乎无法重现。

EDIT 编辑
I pasted the code of the class to here: http://pastebin.com/UeE6qBHx 我将类的代码粘贴到了这里: http : //pastebin.com/UeE6qBHx

I'm pretty sure it is thread safe. 我很确定它是线程安全的。 There are two fields, which are configuration fields and are not modified inside the class. 有两个字段,它们是配置字段,并且不会在类内部进行修改。 So basically this class has no state and all calculation has no side effects (Unless I don't see something very obscure). 因此,基本上这个类没有状态,所有计算都没有副作用(除非我看不到非常晦涩的东西)。

Ref modifier is not needed here, because the reference is not modified. 此处不需要Ref修饰符,因为未修改参考。

There's no way of telling unless you know if it stores values in local variables or in a field (in the static class, not the method). 除非您知道它是否将值存储在局部变量或字段中(在静态类中,而不是方法中),否则无法告诉。

All local variables will be fine and instanced per call, but the fields will not. 所有局部变量都可以,并且可以在每次调用时进行实例化,但是这些字段不能。

A very bad example: 一个非常糟糕的例子:

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

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

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