How to use an AppDomain to limit a static class' scope for thread-safe use?

后端 未结 6 2103
我在风中等你
我在风中等你 2021-01-05 10:31

I have been bitten by a poorly architected solution. It is not thread safe!

I have several shared classes and members in the solution, and during development all

6条回答
  •  醉话见心
    2021-01-05 10:58

    Why not just put a lock around the code you want to execute sequentially? It will be a bottleneck, but it should work in a multithreaded environment.

    public class Loader
    {
        private static object SyncRoot = new object();
        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()
        {
    
            lock(SyncRoot) {
                dataFile = new DataFile(aredStream, fileName, connectionString);
                dataFile.ParseFile();
               dataFile.Save();
            }
    
        }
    
    }
    

提交回复
热议问题