File Access Strategy in a Multi-Threaded Environment (Web App)

前端 未结 5 1120
南笙
南笙 2021-01-05 16:25

I have a file which is an XML representation of some data that is taken from a Web service and cached locally within a Web Application. The idea being is that this data is <

5条回答
  •  粉色の甜心
    2021-01-05 16:53

    How about using AutoResetEvent to communicate between threads? I created a console app which creates roughly 8 GB file in createfile method and then copy that file in main method

     static AutoResetEvent waitHandle = new AutoResetEvent(false);
        static string filePath=@"C:\Temp\test.txt";
        static string fileCopyPath=@"C:\Temp\test-copy.txt";
        static void Main(string[] args)
        {
            Console.WriteLine("in main method");
            Console.WriteLine();
            Thread thread = new Thread(createFile);
            thread.Start();
    
            Console.WriteLine("waiting for file to be processed ");
            Console.WriteLine();
            waitHandle.WaitOne();
            Console.WriteLine();
    
            File.Copy(filePath, fileCopyPath);
            Console.WriteLine("file copied ");
    
        }
    
    
        static void createFile()
        {
    
            FileStream fs= File.Create(filePath);            
            Console.WriteLine("start processing a file "+DateTime.Now);
            Console.WriteLine();
            using (StreamWriter sw = new StreamWriter(fs))
            {
                for (long i = 0; i < 300000000; i++)
                {
                    sw.WriteLine("The value of i is " + i);
    
                }
            }
            Console.WriteLine("file processed " + DateTime.Now);
            Console.WriteLine();
    
            waitHandle.Set();
        }
    

提交回复
热议问题