"how to use an appdomain to limit a static class' scope for thread-safe use?" Code Answer

5

using app domains you could do something like this:

public class loader
{

    private string connectionstring;
    private string filename;
    private stream stream;
    private datafile datafile;

    public loader(stream stream, string filename, string connectionstring)
    {
        this.connectionstring = connectionstring;
        this.filename = filename;
        this.stream = stream;
    }  

    public void process()
    {
        //*****  create appdomain here *****
        string threadid = thread.currentthread.managedthreadid.tostring();
        appdomain appdomain = appdomain.createdomain(threadid);

        datafile datafile = 
            (datafile) appdomain.createinstanceandunwrap(
                        "<datafile assemblyname>", 
                        "datafile", 
                        true, 
                        bindingflags.default,
                        null,
                        new object[] 
                        { 
                            aredstream, 
                            filename, 
                            connectionstring 
                        },
                        null,
                        null,
                        null);
        datafile.parsefile();
        datafile.save();

        appdomain.unload(threadid);       
    }
}
By varundhariyal on June 25 2022

Answers related to “how to use an appdomain to limit a static class' scope for thread-safe use?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.