Question

I'm using a managed C# wrapper to access an unmanaged C++ library. The library does some time consumung caluclations (up to 6 seconds) that I need. But also in parallel to that I continuously need some data that is fast to get too.

To achieve that, I tried to get two instances of my wrapper, one for the quick stuff and the other one in a parallel thread to calculate the time consuming information. But, as soon as I instanciate the slow one of the Analyzers, even the quick one gets slow too.

fullAnalyzer = new Analyzer(FullAnalysis);
miniAnalyzer = new Analyzer(MinimalAnalysis);

It looks like both of them are sharing the same configuration in the back, because if I instanciate the quick one first, it is still fast.

Is it in general possible to have two or more seperate instances of a wrapper accessing an unmanaged library without interfering? I so - how is it done? Or is this behaviour just a local thing of this library?

Edit: This is the constructor and some part of the wrapper code

public class ScrWrapper
{
    private const string DllName = @"Analyzer.dll";

    public bool IsConfigLoaded { get; private set; }
    public bool IsAnalyticsSuccessful { get; private set; }

    public Analyzer()
    {
        IsConfigLoaded = false;
        IsAnalyticsSuccessful = false;
    }

    public Analyzer(string configFileName, ScrProcLevel procLevel = ScrProcLevel.PL_NONE)
    {
        IsConfigLoaded = false;
        IsAnalyticsSuccessful = false;
        LoadConfig(configFileName, procLevel);
    }

    public void LoadConfig(string configFileName, ProcLevel procLevel = ScrProcLevel.PL_NONE)
    {
        if (configFileName.Length < 1)
            throw new ArgumentException("Empty configFileName. Must contain valid file name.");

        if (!System.IO.File.Exists(configFileName))
            throw new ArgumentException(String.Format("Invalid configFileName. File not found: {0}",configFileName));

        if (!System.IO.File.Exists(DllName))
            throw new ArgumentException(String.Format("Invalid DllName. File not found: {0}", DllName));

        bool b_config_status = false;
        try
        {
            StringBuilder sb = new StringBuilder(configFileName);
            ScanAuto_EnableWriteOut(true);
            b_config_status = ScanAuto_LoadConfig(sb);
        }
        catch (Exception ex)
        {
            throw new ScrException("ERROR: Unmanaged Analyzer threw exception.", ex);
        }
        if (!b_config_status)
        {
            throw new ScrException(String.Format("ERROR: Failed to load the configurationfile, b_config_status=false"));
        }

        IsConfigLoaded = b_config_status;

        _ProcLevel = procLevel;
        Analyzer_SetProcLevel(_ProcLevel); 
    }       
...     


    [DllImport(DllName, CallingConvention = CallConvention)]
    [return: MarshalAs(UnmanagedType.I1)]
    private extern static bool ScanAuto_LoadConfig(StringBuilder _pConfigFName);

}
Was it helpful?

Solution

Your wrapper looks fine from what I can tell, so its the fact that ScanAuto_LoadConfig method (and therefore the members it's initialising) is static, causing it to overwrite the same bit of config each time. see Static Data Members (C++)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top